2013-07-25 3 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

가 정적 리소스를 통해 리소스를 직접 전달하는 것은 안정적으로 작동하지 않지만, XAML 요소는 일반적으로 수 재사용 가능한 부품으로 더 세분화되어야한다. 당신이 PathGeometry class declaration 보면, 당신이 가지고 알 수 있습니다 :

[ContentPropertyAttribute("Figures")] 

Figures 속성을 의미하는 것은 실제로 설정됩니다 어떤 경우 XAML에서 당신이 둥지 아이를. 이 속성의 유형은 PathFigureCollection이며 PathFigure 요소가 추가됩니다.

<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

실제 자원 (여기서는 PathGeometry)에 대해 StaticResource을 "프록시"로 사용할 수 없습니다.

각각의 PathGeometry을 해당 <Style.Resources> 섹션으로 이동하십시오. 따라서 당신이 어떤 PathGeometries는 "X_Sign"또는 "Min_Sign"라고하지 않습니다 - 두라고 "geoPath"

... 

<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

네, 그것은 같은 작품을 이렇게하면 자원으로 PathFigureCollection 자체를 저장할 수 있음을 의미 지금 매력. 고맙습니다. –