为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

C#
红糖糍粑 2022-11-22 16:08:46
我有一个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>我使用这个不灵活的代码得到了想要的结果:<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>截屏:如果我尝试使用s 的TargetName属性,Setter我会收到错误消息:XDG0029 | 无法识别名称“MyFirstPath”。XDG0029 | 无法识别名称“MySecondPath”。
查看完整描述

1 回答

?
侃侃尔雅

TA贡献1801条经验 获得超15个赞

这是带触发器的解决方案:我只是测试列号是否为 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>

//img1.sycdn.imooc.com//637c83d70001deca06010331.jpg

查看完整回答
反对 回复 2022-11-22
  • 1 回答
  • 0 关注
  • 76 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信