我很新的WPF。下面是我試圖做的,以便使用相同的controlTemplate
按鈕,它們之間的唯一區別是PathGeometry
值。如何使用相同的按鈕相同的模板
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Shared.xaml" />
</ResourceDictionary.MergedDictionaries>
<Style TargetType="Button" x:Key="buttonHeader">
<Setter Property="Width" Value="18" />
<Setter Property="Height" Value="18" />
<Setter Property="Cursor" Value="Hand" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Border Name="BorderStyle" Background="Transparent" >
<Path
x:Name="CheckMark"
HorizontalAlignment="Center"
VerticalAlignment="Bottom"
SnapsToDevicePixels="False"
Stroke="#FF4D4D4D"
StrokeThickness="2" StrokeEndLineCap="Flat" StrokeStartLineCap="Flat"
Data="{DynamicResource geoPath}">
</Path>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="true">
<Setter TargetName="BorderStyle" Property="Background" Value="#B2FFFFFF" />
<Setter TargetName="CheckMark" Property="Stroke" Value="#D8727272" />
</Trigger>
<Trigger Property="IsPressed" Value="true">
<Setter TargetName="BorderStyle" Property="Background" Value="#B2707070" />
<Setter TargetName="CheckMark" Property="Stroke" Value="#D8FFFFFF" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<PathGeometry x:Key="X_Sign">
<PathFigure StartPoint="0,0">
<LineSegment Point="10,10"/>
</PathFigure>
<PathFigure StartPoint="0,10">
<LineSegment Point="10,0"/>
</PathFigure>
</PathGeometry>
<PathGeometry x:Key="Min_Sign">
<PathFigure StartPoint="0,0">
<LineSegment Point="10,0"/>
</PathFigure>
</PathGeometry>
<Style x:Key="ButtonX" BasedOn="{StaticResource buttonHeader}" TargetType="Button">
<Style.Resources>
<StaticResource x:Key="geoPath" ResourceKey="X_Sign"/>
</Style.Resources>
</Style>
<Style x:Key="ButtonXMinimize" BasedOn="{StaticResource buttonHeader}" TargetType="Button">
<Style.Resources>
<StaticResource x:Key="geoPath" ResourceKey="Min_Sign"/>
</Style.Resources>
</Style>
</ResourceDictionary>
在設計師,我真正得到正是我想要的,但是當我嘗試運行應用程序,我得到一個XamlParseException
和的InnerException是:
無法投類型的對象「系統。 Windows.Media.PathGeometry'鍵入'System.Windows.ResourceDictionary'
我缺少什麼以及如何修復它? 另外,我很樂意知道是否有更好的方法來做到這一點。
在此先感謝。
是的,它就像現在魅力。非常感謝你。 –