绑定到ControlTemplate内的嵌套属性

Ben*_*jol 7 xaml

目的是为我的自定义按钮模板构建图标路径几何的资源字典.

到目前为止有效:

ResourceDictionary中:

<Geometry x:Key="ArrowDown">
    M0,10 M10,0 M5,1 L5,7 4.2,7 5,8 5.8,7 5,7
</Geometry>
<Geometry x:Key="ArrowUp">
    M0,0 M10,10 M5,9 L5,3 4.2,3 5,2 5.8,3 5,3
</Geometry>
Run Code Online (Sandbox Code Playgroud)

路径的附属物:

public static Geometry GetIconPath(UIElement element)
{
    return (Geometry)element.GetValue(IconPathProperty);
}

public static void SetIconPath(UIElement element, Geometry value)
{
    element.SetValue(PIconPathProperty, value);
}

public static readonly DependencyProperty IconPathProperty =
    DependencyProperty.RegisterAttached("IconPath", typeof(Geometry), typeof(Attached));
Run Code Online (Sandbox Code Playgroud)

模板:

<ControlTemplate x:Key="ButtonTemplate" TargetType="{x:Type Button}">
    <Viewbox>
        <Path Name="Icon" Stroke="Black" StrokeThickness="1" Stretch="Uniform"
              Data="{Binding (local:Attached.IconPath), RelativeSource={RelativeSource TemplatedParent}}"/>
    </Viewbox>
    <ControlTemplate.Triggers>
        <Trigger Property="IsEnabled" Value="False">
            <Setter Property="Stroke" TargetName="Icon" Value="Gray"/>
            <Setter TargetName="Icon" Property="Effect">
                <Setter.Value>
                    <DropShadowEffect ShadowDepth="0.2" Color="Black" Direction="125"/>
                </Setter.Value>
            </Setter>
        </Trigger>
    </ControlTemplate.Triggers>
</ControlTemplate>
Run Code Online (Sandbox Code Playgroud)

按钮:

<Button local:Attached.IconPath="{StaticResource ArrowDown}"/>
Run Code Online (Sandbox Code Playgroud)

这样可以正常工作,但我现在增加了一层复杂性:一些图标需要填充而其他图标不需要填充.我想在ResourceDictionary中使用GeometryData获取此信息.

一种解决方案是将整个Path存储在ResourceDictionary中,但正如您在ControlTemplate中看到的,我需要能够访问Path来触发DropShadowEffect.

所以我尝试的替代解决方案是使用POCO将两个元素存储在:

public class IconData
{
    public bool Fill { get; set; }
    public Geometry Geometry { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

所以ResourceDictionary现在包含:

<local:IconData x:Key="ArrowUp" Fill="True" Geometry="
    M0,0 M10,10 M5,9 L5,3 4.2,3 5,2 5.8,3 5,3"/>
Run Code Online (Sandbox Code Playgroud)

使用适当的附加属性,ControlTemplate变为:

<ControlTemplate x:Key="ButtonTemplate" TargetType="{x:Type Button}">
    <Viewbox>
        <Path Name="Icon" Stroke="Black" StrokeThickness="1" Stretch="Uniform"
              Data="{Binding (local:Attached.IconData.Geometry), RelativeSource={RelativeSource TemplatedParent}}"/>
    </Viewbox>
    <ControlTemplate.Triggers>
        <Trigger Property="local:Attached.IconData.Fill" Value="True">
            <Setter Property="Fill" TargetName="Icon" Value="#00000000"/>
        </Trigger>
        <Trigger Property="IsEnabled" Value="False">
            <Setter Property="Stroke" TargetName="Icon" Value="Gray"/>
            <Setter TargetName="Icon" Property="Effect">
                <Setter.Value>
                    <DropShadowEffect ShadowDepth="0.2" Color="Black" Direction="125"/>
                </Setter.Value>
            </Setter>
        </Trigger>
    </ControlTemplate.Triggers>
</ControlTemplate>
Run Code Online (Sandbox Code Playgroud)

不幸的是,这里都提到了IconData Nested types are not supported: Attached.IconData.

我愿意接受我的实施解决方案,或任何解决方法或不同的解决方案.

sto*_*ran 4

<Path Name="Icon" Stroke="Black" StrokeThickness="1" Stretch="Uniform"
      Data="{Binding (local:Attached.IconData.Geometry), RelativeSource={RelativeSource TemplatedParent}}"/>
Run Code Online (Sandbox Code Playgroud)

这里的(local:Attached.IconData.Geometry)意思是嵌套类型。要获取对象的属性,Attached.IconData应该使用以下绑定路径:

<Path Name="Icon" Stroke="Black" StrokeThickness="1" Stretch="Uniform"
      Data="{Binding (local:Attached.IconData).Geometry, RelativeSource={RelativeSource TemplatedParent}}"/>
Run Code Online (Sandbox Code Playgroud)

另请务必将附加属性类型从 in 类型更改为GeometryinIconData类型Attached

IconDataclass 不应该嵌套到任何类型,因为它正是所谓的不支持的类型(只需放入与其他使用的类型相同的命名空间中)。

经过这些修改后,您的代码开始为我工作。

这是主窗口 XAML:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfApplication1"
        Title="MainWindow" Height="250" Width="260">
    <Window.Resources>
        <ResourceDictionary>
            <local:IconData x:Key="ArrowUp" Fill="False" Geometry="M0,0 M10,10 M4.8,9 4.8,3 4.2,3 5,2 5.8,3 5.2,3 5.2,9 Z"/>
            <ControlTemplate x:Key="ButtonTemplate" TargetType="{x:Type Button}">
                <Viewbox>
                    <Path Name="Icon" Stretch="Uniform"
                          Data="{Binding (local:Attached.IconPath).Geometry, RelativeSource={RelativeSource TemplatedParent}}">
                        <Path.Style>
                            <Style TargetType="Path">
                                <Style.Triggers>
                                    <DataTrigger Binding="{Binding (local:Attached.IconPath).Fill, RelativeSource={RelativeSource TemplatedParent}}"
                                                     Value="True">
                                        <Setter Property="Fill" Value="Black" />
                                    </DataTrigger>
                                    <DataTrigger Binding="{Binding (local:Attached.IconPath).Fill, RelativeSource={RelativeSource TemplatedParent}}"
                                                     Value="False">
                                        <Setter Property="Stroke" Value="Black" />
                                        <Setter Property="StrokeThickness" Value="0.2" />
                                    </DataTrigger>
                                </Style.Triggers>
                            </Style>
                        </Path.Style>
                    </Path>
                </Viewbox>
            </ControlTemplate>

        </ResourceDictionary>
    </Window.Resources>

    <Button local:Attached.IconPath="{StaticResource ArrowUp}" Template="{StaticResource ButtonTemplate}" />
</Window>
Run Code Online (Sandbox Code Playgroud)

请注意,我已经修改了路径几何形状以M0,0 M10,10 M4.8,9 4.8,3 4.2,3 5,2 5.8,3 5.2,3 5.2,9 Z使其闭合,从而适合填充。

以下是案例<local:IconData Fill="False" ... />和截图<local:IconData Fill="True" ... />

Fill=False 案例的屏幕截图 Fill=True 情况的屏幕截图