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 が取得されます。

タイプ 'System.Windows.Media.PathGeometry' のオブジェクトをタイプ 'System.Windows.ResourceDictionary' にキャストできません

何が欠けていますか?どうすれば修正できますか? また、何か良い方法があれば教えていただけると幸いです。

前もって感謝します。

4

2 に答える 2

1

StaticResource を介してリソースを直接渡すことは確実には機能しませんが、通常、XAML 要素は再利用可能な部分にさらに分割できます。PathGeometry クラスの宣言を見ると、次のようになっていることがわかります。

[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

于 2013-07-26T00:35:59.730 に答える
0

StaticResourceそのような実際のリソース (ここでは: ) の「プロキシ」として a を使用することはできませんPathGeometry

できることは、それぞれPathGeometryを対応する<Style.Resources>セクションに移動することです。したがって、「X_Sign」または「Min_Sign」と呼ばれる PathGeometry はありません。「geoPath」と呼ばれる 2 つのみです。

...

<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>
于 2013-07-25T22:28:05.723 に答える