【问题标题】:DataTrigger inside ControlTemplate doesn't updateControlTemplate 中的 DataTrigger 不更新
【发布时间】:2011-03-04 20:45:15
【问题描述】:

我有一个绑定到 CustomerViewModel 对象列表的 ListBox,每个都有两个依赖属性:
- 名称(字符串)
- 描述(字符串)
- IsVisible (bool)

(IsVisible 属性默认为 True,可通过 CustomerViewModel 上的 ToggleVisibility 命令反转)

我想在边框控件的右侧显示名称和描述,即当 IsVisible 属性为 True 时具有透明背景,当属性为 False 时具有绿色背景。

我的问题是下面代码的 DataTrigger 部分不能按我想要的方式工作,因为当 IsVisible 更改时不会触发 Setter 部分。

我做错了什么?

这是我的代码:

<UserControl.Resources>
    <Style x:Key="ListBoxStyle" TargetType="{x:Type ListBox}">
        <Setter Property="Margin" Value="-1,-1,0,0" />
        <Setter Property="BorderThickness" Value="0" />
        <Setter Property="Background" Value="Transparent" />
        <Setter Property="ItemContainerStyle" Value="{DynamicResource ListboxItemStyle}" />
        <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Disabled" />
    </Style>

    <Style x:Key="ListboxItemStyle" TargetType="{x:Type ListBoxItem}">
        <Setter Property="Background" Value="Transparent" />
        <Setter Property="FocusVisualStyle" Value="{x:Null}" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ListBoxItem}">
                    <Grid>
                        <Border x:Name="border" Background="{TemplateBinding Background}" BorderBrush="#FFD4D6D5" BorderThickness="0,0,0,1">
                            <Grid Height="70" Margin="0,0,10,0">
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="10" />
                                    <RowDefinition Height="Auto" />
                                    <RowDefinition />
                                    <RowDefinition Height="10" />
                                </Grid.RowDefinitions>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="Auto" />
                                    <ColumnDefinition />
                                </Grid.ColumnDefinitions>
                                <Border x:Name="visibilityColumn" Grid.Row="0" Grid.Column="0" Grid.RowSpan="4" Background="Transparent" Width="4" Margin="0,0,4,0" />
                                <TextBlock x:Name="customerName" Grid.Row="1" Grid.Column="1" Foreground="#FF191919" FontWeight="Bold" Text="{Binding Name}" VerticalAlignment="Top" />
                                <TextBlock Grid.Row="2" Grid.Column="1" VerticalAlignment="Stretch" Text="{Binding Description}" TextWrapping="Wrap" Foreground="#FFB4B4B4" TextTrimming="CharacterEllipsis" />
                            </Grid>
                            <Border.ContextMenu>
                                <ContextMenu>
                                    <MenuItem Header="Edit..." />
                                    <MenuItem Header="Visible" IsCheckable="True" IsChecked="{Binding IsVisible}" Command="{Binding ToggleVisibility}"/>
                                </ContextMenu>
                            </Border.ContextMenu>
                        </Border>
                    </Grid>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter Property="Background" Value="#FFEEEEEE" />
                        </Trigger>
                        <Trigger Property="IsSelected" Value="True">
                            <Setter Property="Background" Value="#FFF5F5F5" />
                            <Setter TargetName="customerName" Property="Foreground" Value="Green" />
                        </Trigger>
                        <DataTrigger Binding="{Binding IsVisible}" Value="False"> <!--If Value="True" the customerName Border shows up green!-->
                            <Setter Property="Background" Value="Green" />
                        </DataTrigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</UserControl.Resources>

<ListBox Style="{StaticResource ListBoxStyle}" ItemsSource="{Binding CustomerViewModels}" />

更新:
正如 Goblin 所指出的,DataTrigger 确实缺少 TargetName="visibilityColumn"。

然而——“真正的”问题是,这一行:

<MenuItem Header="Visible" IsCheckable="True" IsChecked="{Binding IsVisible}" Command="{Binding ToggleVisibility}"/>

可检查的 MenuItem 在 IsChecked 属性上具有 TwoWay 绑定模式,因此我实际上两次反转 IsVisiblity - 通过数据绑定和通过 ToggleVisibility 命令...哎呀 :)

【问题讨论】:

    标签: wpf listbox controltemplate datatrigger listboxitem


    【解决方案1】:

    尝试切换这部分:

    <DataTrigger Binding="{Binding IsVisible}" Value="False"> 
        <Setter Property="Background" Value="Green" />
    </DataTrigger>
    

    这部分:

    <DataTrigger Binding="{Binding IsVisible}" Value="False"> 
        <Setter TargetName="visibilityColumn" Property="Background" Value="Green"  />
    </DataTrigger>
    

    我认为您错过了 setter 中的 TargetName 属性。 (顺便说一下,您的 IsSelected 和 IsMouseOver 触发器也是如此)

    希望这会有所帮助!

    【讨论】:

    • 嗨 - 尝试在 DataTrigger 设置器中添加 TargetName 属性,但不幸的是这没有做任何事情。而且我认为“常规”触发器不需要它,因为它们按预期工作?但我可能是错的:)
    • 哦——你想让它应用到最外面的网格吗?并且您的 NotifyPropertyChanged 事件正在 ViewModel 中触发?很高兴看到围绕 IsVisible 的 ViewModel 实现。
    • 好吧 - 地精的回答是问题的至少一半,所以我将其标记为已接受的答案。原始问题已更新为完整的解决方案。
    猜你喜欢
    • 2012-04-17
    • 2020-09-07
    • 2017-02-19
    • 2011-10-09
    • 1970-01-01
    • 2012-02-14
    • 2018-05-08
    • 2013-11-13
    • 2015-10-07
    相关资源
    最近更新 更多