简体   繁体   English

WPF INotifyDataErrorInfo高亮显示ListBoxItem

[英]WPF INotifyDataErrorInfo highlight ListBoxItem

I've got a ListBox as such: 我有这样一个ListBox

<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 . NetworkAdapters是实现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. 使用当前的XAML,如果任何视图模型中都有错误,则整个ListBox都将突出显示为红色,但我只希望单个包含错误的ListBoxItems突出显示。

I had a look at similar questions such as: 我看过类似的问题,例如:

WPF ListBox ErrorTemplate and Validating a ListBoxItem rather than a ListBox WPF ListBox ErrorTemplate验证ListBoxItem而不是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: 根据Krzysztof的建议,我尝试将StackPanel包裹在边框上,并使用DataTrigger这样:

<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: 但是,产生的结果如下:

在此处输入图片说明

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. 我想围绕整个ListBoxItem突出显示,而不仅仅是图像中的一部分。

You need to implement an ItemContainerStyle as below: 您需要实现一个ItemContainerStyle ,如下所示:

    <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. 这将使您能够更改ListBoxItem本身的边框,因此可以根据需要更改整体。

You can forget about ErrorTemplate and just use DataTrigger to bind to Validation.HasErrors : 您可以忘记ErrorTemplate,而仅使用DataTrigger绑定到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. 如果要突出显示,可以用边框将StackPanel包裹起来,然后将其颜色设置为红色。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM