繁体   English   中英

WPF数据触发器是否要应用标签和子标签?

[英]WPF Data trigger to apply label and child?

我有以下xaml,如果状态为OK,则标签背景为绿色,如果错误标签背景为红色,则对于父标签来说效果很好,但是当我添加子级时,我也希望采用父背景色吗? 有没有一种方法可以让datatrigger充当父标签和子标签?

异端的XAML

<Window x:Class="wpfdemo.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:wpfdemo"
    mc:Ignorable="d"
    Title="MainWindow" Height="50" Width="50" SizeToContent="WidthAndHeight" ResizeMode="NoResize" Topmost="True" AllowsTransparency="True" WindowStyle="None" Background="Transparent">
<ItemsControl ItemsSource="{Binding Path=Parents}" Grid.IsSharedSizeScope="True">
    <ItemsControl.ItemTemplate>
        <DataTemplate DataType="{x:Type local:Parent}">
            <Grid>
                <Grid.ContextMenu>
                    <ContextMenu>
                        <MenuItem Header="Click Me" Click="Button_Click" />
                    </ContextMenu>
                </Grid.ContextMenu>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto" SharedSizeGroup="ParentColumn" />
                    <ColumnDefinition  />
                </Grid.ColumnDefinitions>

                <!-- Parent label -->
                <Label Content="{Binding Path=Name}"
                   x:Name="Label" HorizontalAlignment="Stretch" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" BorderBrush="Black" BorderThickness="1" FontFamily="Verdana" Foreground="White"/>

                <!-- Errors -->
                <ItemsControl ItemsSource="{Binding Path=Errors}"
                          Grid.Column="1">
                    <ItemsControl.ItemTemplate>
                        <DataTemplate DataType="{x:Type local:Child}">
                            <Label Content="{Binding Path=Name}"
                                    HorizontalAlignment="Stretch" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" BorderBrush="Black" BorderThickness="1" FontFamily="Verdana" Foreground="White"/>

                        </DataTemplate>
                    </ItemsControl.ItemTemplate>
                    <ItemsControl.ItemsPanel>
                        <ItemsPanelTemplate>
                            <StackPanel Orientation="Horizontal" />
                        </ItemsPanelTemplate>
                    </ItemsControl.ItemsPanel>
                </ItemsControl>
            </Grid>
            <DataTemplate.Triggers>
                <!-- Parent is ok -->
                <DataTrigger Binding="{Binding Path=Status}"
                         Value="OK">
                    <Setter TargetName="Label" Property="Background" Value="#BF008000" />
                </DataTrigger>

                <!-- Parent is error -->
                <DataTrigger Binding="{Binding Path=Status}"
                         Value="ERROR">
                    <Setter TargetName="Label" Property="Background" Value="#BFFFFF00" />
                    <Setter TargetName="Label" Property="Foreground" Value="Black" />
                </DataTrigger>

            </DataTemplate.Triggers>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

谢谢

TargetName是控件的名称,不是类型。

尝试:

<DataTemplate >
  <Label x:Name="ChildLabel" Content="{Binding Path=Name}"/>
  <DataTemplate.Triggers>
     <DataTrigger Binding="{Binding Path=DataContext.Status,RelativeSource={ RelativeSource Mode=TemplatedParent}}" Value="OK">
        <Setter TargetName="ChildLabel" Property="Background" Value="#BF008000" />
     </DataTrigger>
  </DataTemplate.Triggers>
</DataTemplate>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM