首页 文章

ControlTemplate中的DataTrigger不会更新

提问于
浏览
1

我有一个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}" />

UPDATED:
正如Goblin指出的那样,DataTrigger确实错过了TargetName = "visibilityColumn" .

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

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

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

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-trigger)

    希望这可以帮助!

相关问题