如何在同一样式内使用设置器来更改样式控件内同一类型的不同元素?

我有一个Grid并且里面有两个Path。当在 上应用一种样式时Grid,第一个Path应该有一些Data,第二个应该有一些其他数据。当应用秒样式时,第一个Path应该有另一个Data,第二个Path应该有另一个不同的Data。我希望能够设置Setter的目标元素名称。


我尝试了下面的代码。我在左边得到两个十字而不是一个三角形,在右边得到一个十字。


<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>

http://img3.mukewang.com/637c83ab0001282904960294.jpg

我使用这个不灵活的代码得到了想要的结果:


<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"

        />

</Grid>

截屏:

http://img4.mukewang.com/637c83b8000123ff04890280.jpg

如果我尝试使用s 的TargetName属性,Setter我会收到错误消息:

XDG0029 | 无法识别名称“MyFirstPath”。

XDG0029 | 无法识别名称“MySecondPath”。


红糖糍粑
浏览 84回答 1
1回答

侃侃尔雅

这是带触发器的解决方案:我只是测试列号是否为 0,在这种情况下我显示一个三角形,或者一个十字。<Window.Resources>&nbsp; &nbsp; <Style x:Key="styleWithPlusSign" TargetType="Path">&nbsp; &nbsp; &nbsp; &nbsp; <Style.Triggers>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <Trigger Property="Grid.Column" Value="0">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <Setter&nbsp; Property="Path.Data" Value="M 5,95 L 95,95 50,5 5,95"></Setter>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </Trigger>&nbsp; &nbsp; &nbsp; &nbsp; </Style.Triggers>&nbsp; &nbsp; &nbsp; &nbsp; <Setter Property="Path.Data" Value="M 50,10 L 50,10 L 50,90 M 10,50 L 90,50"></Setter>&nbsp; &nbsp; </Style></Window.Resources><Grid>&nbsp; &nbsp; <Grid.ColumnDefinitions>&nbsp; &nbsp; &nbsp; &nbsp; <ColumnDefinition Width="*"/>&nbsp; &nbsp; &nbsp; &nbsp; <ColumnDefinition Width="*"/>&nbsp; &nbsp; </Grid.ColumnDefinitions>&nbsp; &nbsp; <Path Grid.Column="0" Stroke="Blue"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Stretch="Fill"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; x:Name="MyFirstPath"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Style="{StaticResource styleWithPlusSign}"&nbsp; &nbsp; />&nbsp; &nbsp; <Path Grid.Column="1" Stroke="Black"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; StrokeThickness="4"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Stretch="Uniform"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; x:Name="MySecondPath"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Style="{StaticResource styleWithPlusSign}"/></Grid>如果您需要为每个路径精确列号:<Window.Resources>&nbsp; &nbsp; <Style x:Key="styleWithPlusSign" TargetType="Path">&nbsp; &nbsp; &nbsp; &nbsp; <Style.Triggers>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <Trigger Property="Grid.Column" Value="0">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <Setter&nbsp; Property="Path.Data" Value="M 5,95 L 95,95 50,5 5,95"></Setter>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </Trigger>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <Trigger Property="Grid.Column" Value="1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <Setter Property="Path.Data" Value="M 50,10 L 50,10 L 50,90 M 10,50 L 90,50"></Setter>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </Trigger>&nbsp; &nbsp; &nbsp; &nbsp; </Style.Triggers>&nbsp; &nbsp; </Style></Window.Resources>
打开App,查看更多内容
随时随地看视频慕课网APP