views:

147

answers:

1

I have a ListBox that is bound to a list of CustomerViewModel-objects, that each has two dependency properties:
- Name (string)
- Description (string)
- IsVisible (bool)

(the IsVisible property is True by default and is reversed via the ToggleVisibility Command on the CustomerViewModel)

I would like to display the Name and Description to the right of a Border-control, that is has a Transparent background when the IsVisible property is True and Green when the False.

My problem is that the DataTrigger part of the code below doesn't work the way I want, because the Setter-part isn't triggered when the IsVisible is changed.

What am I doing wrong?

Here's my code:

<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:
The DataTrigger was indeed missing the TargetName="visibilityColumn" as pointed out by Goblin.

However - the "real" problem was, this line:

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

A checkable MenuItem has a TwoWay binding-mode on the IsChecked-property, so I was actually inverting the IsVisiblity twice - via databinding and via the ToggleVisibility command... Whoops :)

A: 

Try switching this part:

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

With this part:

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

I think you missed the TargetName property in your setter. (Same goes for your IsSelected- and IsMouseOver-trigger by the way)

Hope this helps!

Goblin
Hi - tried adding the TargetName property in the DataTrigger setter, but unfortunately that didn't do anything. And I don't think it's needed for the "regular" Triggers as they are working as expected? But I could be wrong :)
kennethkryger
Oh - you want it to apply to the outermost grid? And your NotifyPropertyChanged events are firing in the ViewModel? Would be nice to see the ViewModel implementation around IsVisible.
Goblin
Well - Goblin's answer was a least haft of the problem, so I marked this as the accepted answer. The original question has been updated with the complete solution.
kennethkryger