cogumel0
cogumel0

Reputation: 2681

WPF INotifyDataErrorInfo highlight ListBoxItem

I've got a ListBox as such:

<ListBox Margin="5" ItemsSource="{Binding NetworkAdapters, Mode=OneWay}" SelectedItem="{Binding SelectedNetworkAdapter}" SelectionChanged="{s:Action SelectedNetworkAdapterChanged}">
    <ListBox.ItemsPanel>
        <ItemsPanelTemplate>
            <UniformGrid Columns="2" VerticalAlignment="Top"/>
        </ItemsPanelTemplate>
    </ListBox.ItemsPanel>
    <ListBox.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal">
                <Ellipse Width="15" Height="15" Margin="5">
                    <Ellipse.Style>
                        <Style TargetType="Ellipse">
                            <Setter Property="Fill" Value="Gray"></Setter>
                            <Style.Triggers>
                                <DataTrigger Binding="{Binding Status}" Value="{x:Static wpf:NetworkAdapterStatus.Up}">
                                    <Setter Property="Fill" Value="Green"></Setter>
                                </DataTrigger>
                                <DataTrigger Binding="{Binding Status}" Value="{x:Static wpf:NetworkAdapterStatus.Down}">
                                    <Setter Property="Fill" Value="Red"></Setter>
                                </DataTrigger>
                            </Style.Triggers>
                        </Style>
                    </Ellipse.Style>
                </Ellipse>
                <StackPanel Margin="5,0,0,0">
                    <TextBlock Text="{Binding Name}" FontWeight="Bold"/>
                    <TextBlock Text="{Binding Description}"></TextBlock>
                    <TextBlock Text="{Binding Speed, StringFormat='Speed: {0}'}" FontSize="10"/>
                    <TextBlock Text="{Binding Status}" FontSize="10"/>
                </StackPanel>
            </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

NetworkAdapters is a collection of View Models that implement INotifyDataErrorInfo.

With the current XAML, if there is an error in any of the View Models the whole ListBox will be highlighted red, but I would like just the single ListBoxItems that contains errors to be highlighted.

I had a look at similar questions such as:

WPF ListBox ErrorTemplate and Validating a ListBoxItem rather than a ListBox

But I still can't make this work. Any help would be appreciated.


UPDATE:

As per Krzysztof's advice, I tried wrapping the StackPanel around a border and using a DataTrigger as such:

<ListBox.ItemTemplate>
    <DataTemplate>
        <Border BorderThickness="1">
            <Border.Resources>
                <Style TargetType="Border">
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding HasErrors}" Value="True">
                            <Setter Property="BorderBrush" Value="Red"/>
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </Border.Resources>
            <StackPanel> ... </StackPanel>
        </Border>
    </DataTemplate>
</ListBox.ItemTemplate>

However, what this produces is the following:

enter image description here

Which is slightly different from what I expected. I would like to have the highlight around the whole ListBoxItem not just part of it as per the image.

Upvotes: 0

Views: 393

Answers (2)

Coops
Coops

Reputation: 301

You need to implement an ItemContainerStyle as below:

    <ListBox.ItemContainerStyle>
        <Style TargetType="ListBoxItem">
            <Setter Property="BorderThickness" Value="1" />
            <Setter Property="BorderBrush" Value="Transparent" />
            <Style.Triggers>
                <DataTrigger Binding="{Binding Validation.HasErrors}" Value="True">
                    <Setter Property="BorderBrush" Value="Red"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </ListBox.ItemContainerStyle>

This will enable you to change the border of the ListBoxItem itself, so the whole things as you want.

Upvotes: 1

Krzysztof Skowronek
Krzysztof Skowronek

Reputation: 2946

You can forget about ErrorTemplate and just use DataTrigger to bind to Validation.HasErrors:

  <StackPanel>
                        <StackPanel.Resources>
                            <Style TargetType="{x:Type StackPanel}" BasedOn="{StaticResource {x:Type StackPanel}}">
                                <Style.Triggers>
                                    <DataTrigger Binding="{Binding Validation.HasErrors}" Value="True"> <!-- change all text to red -->
                                        <Setter Property="Foreground" Value="Red"/>
                                    </DataTrigger>
                                </Style.Triggers>
                            </Style>
                        </StackPanel.Resources>
</StackPanel>

If you want a highlight, you can wrap StackPanel with a Border and set its color to red in a style.

Upvotes: 0

Related Questions