WPF INotifyDataErrorInfo 突出显示 ListBoxItem

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

使用当前的 XAML,如果任何视图模型中存在错误,则整个视图模型ListBox将突出显示为红色,但我希望只ListBoxItems突出显示包含错误的单个。


我看过类似的问题,例如:


WPF ListBox ErrorTemplate和 验证 ListBoxItem 而不是 ListBox


但我仍然无法完成这项工作。任何帮助,将不胜感激。


炎炎设计
浏览 186回答 2
2回答

波斯汪

您需要实现ItemContainerStyle如下:&nbsp; &nbsp; <ListBox.ItemContainerStyle>&nbsp; &nbsp; &nbsp; &nbsp; <Style TargetType="ListBoxItem">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <Setter Property="BorderThickness" Value="1" />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <Setter Property="BorderBrush" Value="Transparent" />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <Style.Triggers>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <DataTrigger Binding="{Binding Validation.HasErrors}" Value="True">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <Setter Property="BorderBrush" Value="Red"/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </DataTrigger>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </Style.Triggers>&nbsp; &nbsp; &nbsp; &nbsp; </Style>&nbsp; &nbsp; </ListBox.ItemContainerStyle>这将使您能够更改ListBoxItem自身的边框,从而使整个事物随心所欲。

森栏

您可以忘记 ErrorTemplate 并仅使用 DataTrigger 绑定到Validation.HasErrors:&nbsp; <StackPanel>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <StackPanel.Resources>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <Style TargetType="{x:Type StackPanel}" BasedOn="{StaticResource {x:Type StackPanel}}">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <Style.Triggers>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <DataTrigger Binding="{Binding Validation.HasErrors}" Value="True"> <!-- change all text to red -->&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <Setter Property="Foreground" Value="Red"/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </DataTrigger>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </Style.Triggers>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </Style>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </StackPanel.Resources></StackPanel>如果你想要一个高亮,你可以用一个 Border 包裹 StackPanel 并在一个样式中将它的颜色设置为红色。
打开App,查看更多内容
随时随地看视频慕课网APP