如何在控件绑定中引用向量图形

时间:2013-08-30 21:00:25

标签: c# wpf xaml dependency-properties vector-graphics

全部,我有以下资源定义自定义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来支持此

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"以使用我上面概述的矢量图形?

1 个答案:

答案 0 :(得分: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>