Textblock 样式 dataTrigger 在 ItemsControl 内不起作用

发布于 2024-10-15 16:27:53 字数 1650 浏览 7 评论 0原文

我有一个 ObservableCollection 类型(下面代码中的 Messages),它绑定到 ItemsControl。 Object1 有两个属性,即 ErrMsgIsError。如果错误(即,如果 IsError 为 true),我想以红色显示 ErrMsg,否则显示黑色。

<ItemsControl
    Height="Auto"
    Background="White"
    ItemsSource="{Binding Messages}">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <StackPanel>
                <TextBlock
                    Margin="5,0,0,0"
                    Text="{Binding ErrMsg}"
                    Width="Auto"
                    Foreground="Black">
                    <TextBlock.Style>  
                        <Style TargetType="{x:Type TextBlock}">       
                            <Style.Triggers>         
                                <DataTrigger
                                    Binding="{Binding IsError}"
                                    Value="true">      
                                    <Setter
                                        Property="TextBlock.Foreground"
                                        Value="Red" />         
                                </DataTrigger>       
                            </Style.Triggers>     
                        </Style>   
                    </TextBlock.Style> 
                </TextBlock>
            </StackPanel>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

问题是,无论 IsError 值如何,所有消息始终以黑色显示?

我怎样才能实现这个目标?

I have an ObservableCollection<Object1> type(Messages in code below) which is bound to an ItemsControl. Object1 is having two properties namely ErrMsg and IsError. I want to display the ErrMsg in red colour if its an Error(i.e. if IsError is true) otherwise Black.

<ItemsControl
    Height="Auto"
    Background="White"
    ItemsSource="{Binding Messages}">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <StackPanel>
                <TextBlock
                    Margin="5,0,0,0"
                    Text="{Binding ErrMsg}"
                    Width="Auto"
                    Foreground="Black">
                    <TextBlock.Style>  
                        <Style TargetType="{x:Type TextBlock}">       
                            <Style.Triggers>         
                                <DataTrigger
                                    Binding="{Binding IsError}"
                                    Value="true">      
                                    <Setter
                                        Property="TextBlock.Foreground"
                                        Value="Red" />         
                                </DataTrigger>       
                            </Style.Triggers>     
                        </Style>   
                    </TextBlock.Style> 
                </TextBlock>
            </StackPanel>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

Problem is that all the messages are always displayed in Black colour irrespective of IsError value?

How can I achieve this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

べ繥欢鉨o。 2024-10-22 16:27:53

这是因为您在文本块声明中指定了 Foreground="Black"。本地值(在元素本身上设置)覆盖样式值(包括触发器)。

要解决此问题,只需将黑色前景的设置移至样式:

<TextBlock Margin="5,0,0,0"
           Text="{Binding Value}"
           Width="Auto">
    <TextBlock.Style>
        <Style TargetType="{x:Type TextBlock}">
            <Setter Property="Foreground"
                    Value="Black"/>
            <Style.Triggers>
                <DataTrigger Binding="{Binding IsError}"
                             Value="true">
                    <Setter Property="Foreground"
                            Value="Red" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </TextBlock.Style>
</TextBlock>

That's because you specify Foreground="Black" in your text block declaration. Local values (set on the element itself) override style values (including triggers).

To fix this, just move setting of black foreground to the style:

<TextBlock Margin="5,0,0,0"
           Text="{Binding Value}"
           Width="Auto">
    <TextBlock.Style>
        <Style TargetType="{x:Type TextBlock}">
            <Setter Property="Foreground"
                    Value="Black"/>
            <Style.Triggers>
                <DataTrigger Binding="{Binding IsError}"
                             Value="true">
                    <Setter Property="Foreground"
                            Value="Red" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </TextBlock.Style>
</TextBlock>
深居我梦 2024-10-22 16:27:53

我认为您只需要从属性中删除 TextBlock 前缀,并在样式中将前景设置为黑色:

  <Style TargetType="{x:Type TextBlock}"> 
     <Setter Property="Foreground" Value="Black"/>
     <Style.Triggers>         
         <DataTrigger Binding="{Binding IsError}" Value="true">      
             <Setter Property="Foreground" Value="Red" />         
         </DataTrigger>       
     </Style.Triggers>   
  </Style>

您通常只需要使用类型(应在括号中)来限定属性包含附加属性或故事板的路径。

I think you just need to remove the TextBlock prefix from your property, and set the foreground to black in the style:

  <Style TargetType="{x:Type TextBlock}"> 
     <Setter Property="Foreground" Value="Black"/>
     <Style.Triggers>         
         <DataTrigger Binding="{Binding IsError}" Value="true">      
             <Setter Property="Foreground" Value="Red" />         
         </DataTrigger>       
     </Style.Triggers>   
  </Style>

You typically only need to qualify the property with a type (which should be in parenthesis) for paths that include attached properties, or for storyboards.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文