如何引用一个向量图形的控制绑定(How to Reference a Vectored Graphi

2019-10-18 13:48发布

所有的,我有以下的资源定义自定义CheckBox和图像以用于检查/取消选中状态。

<sys:String x:Key="Up">
    F1 M 37.8516,35.625L 34.6849,38.7917L 23.6016,50.2708L 
23.6016,39.9792L 37.8516,24.9375L 52.1016,39.9792L 52.1016,
50.2708L 41.0182,38.7917L 37.8516,35.625 Z
</sys:String>

<sys:String x:Key="Down">
    F1 M 37.8516,39.5833L 52.1016,24.9375L 52.1016,35.2292L 
37.8516,50.2708L 23.6016,35.2292L 23.6016,24.9375L 37.8516,39.5833 Z
</sys:String>

<Style x:Key="styleCustomCheckBox" TargetType="{x:Type CheckBox}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type CheckBox}">
                <StackPanel Orientation="Horizontal">
                    <Path x:Name="MyPin" Width="18" Height="18" Stretch="Fill" Fill="#FF000000" 
                      Data="{Binding RelativeSource={RelativeSource TemplatedParent}, 
                                                     Path=(local:CustomCheckBoxClass.IsCheckedOnData)}" />
                    <ContentPresenter VerticalAlignment="Center" Margin="10,0,0,0" />
                </StackPanel>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsChecked" Value="False">
                        <Setter TargetName="MyPin" Property="Data"
                            Value="{Binding RelativeSource={RelativeSource TemplatedParent}, 
                                                            Path=(local:CustomCheckBoxClass.IsCheckedOffData)}" />
                        <Setter TargetName="MyPin" Property="Fill" Value="Gray" />
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

如果是这种使用方法如下

<StackPanel Orientation="Vertical" HorizontalAlignment="Center" Background="Beige">
    <CheckBox Height="35" 
          local:CustomCheckBoxClass.IsCheckedOnData="{StaticResource Up}"
          local:CustomCheckBoxClass.IsCheckedOffData="{StaticResource Down}"
          Style="{StaticResource styleCustomCheckBox}" 
          Content="MySolution1" />

    <CheckBox Height="35" 
          local:CustomCheckBoxClass.IsCheckedOnData="{StaticResource Up}"
          local:CustomCheckBoxClass.IsCheckedOffData="{StaticResource Down}"
          Style="{StaticResource styleCustomCheckBox}" 
          Content="MySolution2" />
</StackPanel>

我用下面DependencyProperty s到支持此

public class CustomCheckBoxClass : DependencyObject
{
    public static readonly DependencyProperty IsCheckedOnDataProperty;
    public static void SetIsCheckedOnData(DependencyObject DepObject, string value)
    {
        DepObject.SetValue(IsCheckedOnDataProperty, value);
    }

    public static string GetIsCheckedOnData(DependencyObject DepObject)
    {
        return (string)DepObject.GetValue(IsCheckedOnDataProperty);
    }

    public static readonly DependencyProperty IsCheckedOffDataProperty;
    public static void SetIsCheckedOffData(DependencyObject DepObject, string value)
    {
        DepObject.SetValue(IsCheckedOffDataProperty, value);
    }

    public static string GetIsCheckedOffData(DependencyObject DepObject)
    {
        return (string)DepObject.GetValue(IsCheckedOffDataProperty);
    }

    static CustomCheckBoxClass()
    {
        PropertyMetadata MyPropertyMetadata = new PropertyMetadata(string.Empty);
        IsCheckedOnDataProperty = DependencyProperty.RegisterAttached(
            "IsCheckedOnData", typeof(string), typeof(CustomCheckBoxClass), MyPropertyMetadata);
        IsCheckedOffDataProperty = DependencyProperty.RegisterAttached(
            "IsCheckedOffData", typeof(string), typeof(CustomCheckBoxClass), MyPropertyMetadata);
    }
}

现在,我想知道如何延长这个,所以我可以用更详细的矢量图形。 我有使用Expression Design中创建了一些图形和我已经出口这些为XAML文件。 标记为这些文件之一是

<DrawingBrush x:Key="Layer_1" Stretch="Uniform">
    <DrawingBrush.Drawing>
        <DrawingGroup>
            <DrawingGroup.Children>
                <GeometryDrawing Brush="#FF565656" Geometry="M 107.455,171.715L 107.455,29.3347L 220.225,29.3347L 220.225,171.715L 107.455,171.715 Z "/>
                <GeometryDrawing Brush="#FF565656" Geometry="F1 M 152.5,298.345L 152.5,201.955L 175.18,201.955L 175.18,298.345L 152.5,298.345 Z "/>
                <GeometryDrawing Brush="#FFFFFFFF" Geometry="M 124.15,172.975L 124.15,48.8647L 165.73,48.8647L 165.73,172.975L 124.15,172.975 Z "/>
                <GeometryDrawing Brush="#FF565656" Geometry="F1 M 83.1999,208.885L 83.1999,162.895L 244.48,162.895L 244.48,208.885L 83.1999,208.885 Z "/>
            </DrawingGroup.Children>
        </DrawingGroup>
    </DrawingBrush.Drawing>
</DrawingBrush>

我怎么能包括在此,可以直接在窗口/组件使用的资源字典? 我试图改变DependencyProperty提供访问DrawingBrush和我已经改变了对XAML

<CheckBox Height="35" 
          local:CustomCheckBoxClass.IsCheckedOnData="{StaticResource Layer_1}"
          local:CustomCheckBoxClass.IsCheckedOffData="{StaticResource Down}"
          Style="{StaticResource styleCustomCheckBox}" 
          Content="MySolution1" />

当我引用相关ResourceDictionary ,但是这并没有奏效。

谢谢你的时间。


编辑。 @ HighCore的评论基础上的。 可以说我有以下的自定义Checkbox

<Style x:Key="MheckBox" 
         TargetType="{x:Type CheckBox}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type CheckBox}">
                <StackPanel Orientation="Horizontal">
                    <Image x:Name="imageCheckBox" 
                                 Width="16" 
                                 Height="16" 
                                 Source="F:\Camus\ResourceStudio\Graphics\Images\UnPinned16.png"/>
                    <ContentPresenter VerticalAlignment="Center"/>
                </StackPanel>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsChecked" Value="False">
                        <Setter TargetName="imageCheckBox" 
                                      Property="Source" 
                                      Value="F:\Camus\ResourceStudio\Graphics\Images\Pinned16.png"/>
                    </Trigger>
                    <Trigger Property="IsChecked" Value="True">
                        <Setter TargetName="imageCheckBox" 
                                      Property="Source"
                                      Value="F:\Camus\ResourceStudio\Graphics\Images\UnPinned16.png"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

伟大的工程。 我怎样才能改变Value="F:\Camus\ResourceStudio\Graphics\Images\UnPinned16.png"用我上面列出的矢量图形?

Answer 1:

事实上,该在Template用于Path并假设在一个字符串值Data参数。 但现在你使用DrawingBrush ,通常它被用于Rectangle控制。

所以,我们改变的类型取决于附加属性的类型DrawingBrush

public static readonly DependencyProperty IsCheckedOnDataProperty;

public static void SetIsCheckedOnData(DependencyObject DepObject, DrawingBrush value)
{
    DepObject.SetValue(IsCheckedOnDataProperty, value);
}

public static DrawingBrush GetIsCheckedOnData(DependencyObject DepObject)
{
    return (DrawingBrush)DepObject.GetValue(IsCheckedOnDataProperty);
}

static CustomCheckBoxClass()
{
    PropertyMetadata MyPropertyMetadata = new PropertyMetadata(null);

    IsCheckedOnDataProperty = DependencyProperty.RegisterAttached("IsCheckedOnData",
                                                            typeof(DrawingBrush),
                                                            typeof(CustomCheckBoxClass),
                                                            MyPropertyMetadata);
}

Style ,现在改变Path上的Rectangle ,就像这样:

<Style x:Key="styleCustomCheckBox" TargetType="{x:Type CheckBox}">
    <Setter Property="FontFamily" Value="Verdana" />
    <Setter Property="FontSize" Value="14" />

    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type CheckBox}">
                <StackPanel Orientation="Horizontal">
                    <Rectangle Width="16" Height="16" 
                               Fill="{Binding RelativeSource={RelativeSource TemplatedParent}, 
                               Path=(local:CustomCheckBoxClass.IsCheckedOnData)}" />   

                    <ContentPresenter VerticalAlignment="Center" Margin="10,0,0,0" />
                </StackPanel>

                 <ControlTemplate.Triggers>
                     <Trigger Property="IsChecked" Value="False">
                         <Setter TargetName="MyRectangle" Property="Fill" Value="{Binding RelativeSource={RelativeSource TemplatedParent}, 
                                 Path=(local:CustomCheckBoxClass.IsCheckedOffData)}" />
                     </Trigger>
                 </ControlTemplate.Triggers>
             </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>


文章来源: How to Reference a Vectored Graphic in Control Binding