ControlTemplate中的DataTrigger不会更新

时间:2010-06-17 08:58:45

标签: wpf listbox controltemplate datatrigger listboxitem

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

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

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

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

我做错了什么?

这是我的代码:

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

1 个答案:

答案 0 :(得分: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-trigger也一样)

希望这有帮助!