1

所有,我有以下资源,这些资源定义了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>

我使用以下DependencyPropertys 来支持这一点

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

4

1 回答 1

1

事实上, inTemplate用于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中,现在更改PathRectangle,如下所示:

<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>
于 2013-08-30T22:24:20.307 回答