1

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

(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 命令...哎呀 :)

4

1 回答 1

2

尝试切换这部分:

<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 触发器也是如此)

希望这可以帮助!

于 2010-06-17T09:27:08.167 回答