目的是为我的自定义按钮模板建立一个图标路径几何形状的资源字典。
到目前为止有效的方法:
资源字典:
<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>
路径的附加属性:
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));
模板:
<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>
按钮:
<Button local:Attached.IconPath="{StaticResource ArrowDown}"/>
效果很好,但是我现在增加了一层复杂性:有些图标需要填充,而有些则不需要。而且我想在带有GeometryData的ResourceDictionary中获得此信息。
一种解决方案是将整个Path存储在ResourceDictionary中,但是正如您在ControlTemplate中所看到的那样,我需要能够访问Path来触发DropShadowEffect。
因此,我正在尝试的替代解决方案是使用POCO将两个元素存储在以下位置:
public class IconData
{
public bool Fill { get; set; }
public Geometry Geometry { get; set; }
}
因此,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"/>
使用适当的附加属性,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>
不幸的是,这里两个对IconData的引用都说
Nested types are not supported: Attached.IconData
。我欢迎实现解决方案,任何解决方法或其他解决方案。
最佳答案
<Path Name="Icon" Stroke="Black" StrokeThickness="1" Stretch="Uniform"
Data="{Binding (local:Attached.IconData.Geometry), RelativeSource={RelativeSource TemplatedParent}}"/>
这里
(local:Attached.IconData.Geometry)
表示嵌套类型。要获得Attached.IconData
对象的属性,应使用以下绑定(bind)路径:<Path Name="Icon" Stroke="Black" StrokeThickness="1" Stretch="Uniform"
Data="{Binding (local:Attached.IconData).Geometry, RelativeSource={RelativeSource TemplatedParent}}"/>
另外,请确保将附加属性类型从
Geometry
更改为IconData
类型的Attached
。不应将
IconData
类嵌套为任何类型,因为据说这正是不支持的类型(只需将其与其他使用的类型放在同一命名空间中)。这些修改之后,您的代码就开始为我工作。
这是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>
请注意,我已经修改了
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" ... />
情况的屏幕截图:关于xaml - 绑定(bind)到ControlTemplate中的嵌套属性,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49323774/