【问题标题】:Data Trigger won't trigger background change数据触发器不会触发背景变化
【发布时间】:2019-07-24 17:37:01
【问题描述】:

我有一个 TouchListBoxItem,当某个数据为“假”时,我正在尝试更改每个“框”的背景

我想更改触摸列表框中某些项目的背景颜色,这些项目有一个布尔数据,如果它应该改变背景应该触发

<c:TouchListBox.ItemContainerStyle>
    <Style
        TargetType="ListBoxItem"
        BasedOn="{StaticResource noMouseOverListBoxItemStyle}">
        <Setter
            Property="FocusVisualStyle"
            Value="{StaticResource FocusVisualStyle}" />
        <Style.Triggers>
            <DataTrigger
            Binding="{Binding IsGroupingItem, RelativeSource={RelativeSource FindAncestor, AncestorType=ListBoxItem}}"
            Value="False">
             <Setter
                Property="Background"
                Value="{StaticResource ItemBackgroundColorBrush}" />
                </DataTrigger>
                </Style.Triggers>
    </Style>
</c:TouchListBox.ItemContainerStyle>

添加了 Style.Triggers,如果 IsGroupingItem 为 False,则应将背景更改为 ItemBackgroundColorBrush。

【问题讨论】:

    标签: wpf xaml data-binding


    【解决方案1】:

    表达式

    {Binding IsGroupingItem, RelativeSource={RelativeSource AncestorType=ListBoxItem}}
    

    在 ListBoxItem 类中需要一个 IsGroupingItem 属性,但它没有。

    通过

    绑定到实际项目对象的属性
    Binding="{Binding IsGroupingItem}"
    

    还要确保 IsGroupingItem 实际上是公共属性,而不是字段,并且它会触发 INotifyPropertyChanged 接口的 PropertyChanged 事件。

    【讨论】:

    • 谢谢,这成功了!这值得一票,但我仍然是 stackoverflow 的菜鸟
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-14
    • 2019-01-05
    • 2014-07-21
    • 2019-08-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多