同じスタイル内のセッターを使用して、スタイル付きコントロール内の同じタイプの要素を異なる方法で変更するにはどうすればよいですか?

シルビウボガン

私はGridとその中に2つありPathます。一つのスタイルが上で適用されるとGrid、最初はPathいくつか持っている必要がありData、第二いくつかの他のデータを。秒スタイルが適用される場合、最初のスタイルにはPath別のが必要Dataであり、2番目のスタイルにはPath別の別のが必要Dataです。Setterのターゲット要素名を設定できるようにしたいと思います。

以下のコードを試してみました。左側に1つの三角形、右側に1つの十字の代わりに、2つの十字が表示されます。

<Window x:Class="cs_wpf_test_2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:cs_wpf_test_2"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Window.Resources>
        <Style x:Key="styleWithPlusSign" TargetType="Path">
            <Setter Property="Path.Data" Value="M 5,95 L 95,95 50,5 5,95"></Setter>
            <Setter Property="Path.Data" Value="M 50,10 L 50,10 L 50,90 M 10,50 L 90,50"></Setter>
        </Style>
    </Window.Resources>
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
        <Path Stroke="Blue"
                Stretch="Fill"
                x:Name="MyFirstPath"
              Style="{StaticResource styleWithPlusSign}"
              />
        <Path Grid.Column="1" Stroke="Black"
                StrokeThickness="4"
                Stretch="Uniform"
                x:Name="MySecondPath"
              Style="{StaticResource styleWithPlusSign}"/>
    </Grid>
</Window>

実結果

この柔軟性のないコードを使用して、希望する結果が得られます。

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>
    <Path Stroke="Blue"
        Stretch="Fill"
        x:Name="MyFirstPath"
        Data="M 5,95 L 95,95 50,5 5,95"
        />
    <Path Grid.Column="1" Stroke="Black"
        StrokeThickness="4"
        Stretch="Uniform"
        x:Name="MySecondPath"
        Data="M 50,10 L 50,10 L 50,90 M 10,50 L 90,50"/>
</Grid>

スクリーンショット:

希望する結果

TargetName属性を使用しようとすると、Setterエラーが発生します。

XDG0029 | 「MyFirstPath」という名前は認識されません。

XDG0029 | 「MySecondPath」という名前は認識されません。

更新

不明瞭でごめんなさい。私は2つのスタイル、などをしたいstyleWithPlusSignstyleWithMinusSign

  • styleWithPlusSignアップ指向角と右にあるクロスで三角形を設定する必要があります。
  • styleWithMinusSignダウン指向角と右にあるマイナス記号(ライン)で三角形を設定する必要があります。
フレンチー

トリガーを使用した解決策は次のとおりです。列番号が0かどうかをテストします。この場合は、三角形または十字を表示します。

<Window.Resources>
    <Style x:Key="styleWithPlusSign" TargetType="Path">
        <Style.Triggers>
            <Trigger Property="Grid.Column" Value="0">
                <Setter  Property="Path.Data" Value="M 5,95 L 95,95 50,5 5,95"></Setter>
            </Trigger>
        </Style.Triggers>
        <Setter Property="Path.Data" Value="M 50,10 L 50,10 L 50,90 M 10,50 L 90,50"></Setter>
    </Style>
</Window.Resources>
<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>
    <Path Grid.Column="0" Stroke="Blue"
          Stretch="Fill"
          x:Name="MyFirstPath"
          Style="{StaticResource styleWithPlusSign}"
    />
    <Path Grid.Column="1" Stroke="Black"
          StrokeThickness="4"
          Stretch="Uniform"
          x:Name="MySecondPath"
          Style="{StaticResource styleWithPlusSign}"/>
</Grid>


各パスの列番号を正確にする必要がある場合:

<Window.Resources>
    <Style x:Key="styleWithPlusSign" TargetType="Path">
        <Style.Triggers>
            <Trigger Property="Grid.Column" Value="0">
                <Setter  Property="Path.Data" Value="M 5,95 L 95,95 50,5 5,95"></Setter>
            </Trigger>
            <Trigger Property="Grid.Column" Value="1">
                <Setter Property="Path.Data" Value="M 50,10 L 50,10 L 50,90 M 10,50 L 90,50"></Setter>
            </Trigger>
        </Style.Triggers>
    </Style>
</Window.Resources>

ここに画像の説明を入力してください

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

Related 関連記事

ホットタグ

アーカイブ