2013-07-25 116 views
1

我很新的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'

我缺少什么以及如何修复它? 另外,我很乐意知道是否有更好的方法来做到这一点。

在此先感谢。

回答

1

虽然直接传递资源的StaticResource通过不可靠地工作,XAML元素通常可以进一步分解成可重用的部分。如果你看一下PathGeometry class declaration,你会注意到它:

[ContentPropertyAttribute("Figures")] 

这意味着Figures财产实际上是被设定什么,当你窝孩子在XAML。此属性的类型为PathFigureCollection,您的PathFigure元素将被添加到该属性中。这意味着你可以自己储存PathFigureCollection作为一种资源:

<PathFigureCollection x:Key="XSignFigures"> 
    <PathFigure StartPoint="0,0"> 
     <LineSegment Point="10,10"/> 
    </PathFigure> 
    <PathFigure StartPoint="0,10"> 
     <LineSegment Point="10,0"/> 
    </PathFigure> 
</PathFigureCollection> 

,然后将其应用到PathGeometry当你需要它:

<Style x:Key="ButtonX" BasedOn="{StaticResource buttonHeader}" TargetType="Button"> 
    <Style.Resources> 
     <PathGeometry x:Key="geoPath" Figures="{StaticResource ResourceKey=XSignFigures}" /> 
    </Style.Resources> 
</Style> 

更多信息:http://msdn.microsoft.com/en-us/library/ms788723.aspx#collection_syntax

0

您不能将StaticResource用作实际资源(此处为:PathGeometry)的“代理”。

你可以做的是将每个PathGeometry移动到其相应的<Style.Resources>部分。因此,你不会有任何所谓PathGeometries“X_Sign”或“Min_Sign” - 只是两块被称为“地理路径”:

... 

<Style x:Key="ButtonX" BasedOn="{StaticResource buttonHeader}" TargetType="Button"> 
    <Style.Resources> 
     <!--<StaticResource x:Key="geoPath" ResourceKey="X_Sign"/>--> 
     <PathGeometry x:Key="geoPath"> 
      <PathFigure StartPoint="0,0"> 
       <LineSegment Point="10,10"/> 
      </PathFigure> 
      <PathFigure StartPoint="0,10"> 
       <LineSegment Point="10,0"/> 
      </PathFigure> 
     </PathGeometry> 
    </Style.Resources> 
</Style> 
<Style x:Key="ButtonXMinimize" BasedOn="{StaticResource buttonHeader}" TargetType="Button"> 
    <Style.Resources> 
     <ResourceDictionary> 
      <!--<StaticResource x:Key="geoPath" ResourceKey="Min_Sign"/>--> 
      <PathGeometry x:Key="geoPath"> 
       <PathFigure StartPoint="0,0"> 
        <LineSegment Point="10,0"/> 
       </PathFigure> 
      </PathGeometry> 
     </ResourceDictionary> 
    </Style.Resources> 
</Style> 
+0

是的,它就像现在魅力。非常感谢你。 –