在我的 WPF 应用程序中,我有一个 ListBox 绑定到一组视图模型。这些视图模型通过实现 INotifyDataErrorInfo 来支持验证。我正在尝试在我的 ListBox 中显示具有验证错误的项目的错误模板。
通过在 ListBox的 ItemSource 绑定上设置NotifyOnValidationError=True ,我可以让 ListBox 显示默认错误模板。
看起来像这样:
我的列表框代码:
<ListBox x:Name="ListBoxEvents" ItemsSource="{Binding Events, UpdateSourceTrigger=PropertyChanged, NotifyOnValidationError=True}"
IsSynchronizedWithCurrentItem="True" ItemTemplate="{DynamicResource EventListTemplate}"></ListBox>
我的列表框样式:
<ControlTemplate x:Key="ListBoxValidationError">
<DockPanel LastChildFill="True">
<Border Background="Red" Margin="5">
<AdornedElementPlaceholder />
</Border>
</DockPanel>
</ControlTemplate>
<Style TargetType="{x:Type ListBox}">
<Setter Property="BorderBrush" Value="{StaticResource WindowTitleBrush}" />
<Setter Property="BorderThickness" Value="1" />
<Setter Property="MinWidth" Value="200" />
<Setter Property="HorizontalContentAlignment" Value="Stretch"></Setter>
<Setter Property="ScrollViewer.CanContentScroll" Value="False"></Setter>
<Setter Property="Validation.ErrorTemplate" Value="{StaticResource ListBoxValidationError}"></Setter>
</Style>
ListBox 项模板:
<DataTemplate x:Key="EventListTemplate" DataType="{x:Type event:EventViewModel}">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<TextBlock Text="{Binding Title, Converter={StaticResource EmptyStringConverter}, ConverterParameter='-'}" FontWeight="Bold" FontSize="14" />
<TextBlock Text="{Binding Date, StringFormat={}{0:dd.MM.yyyy}}" Grid.Row="1" Grid.Column="0" FontStyle="Italic" />
<Button Grid.Column="1" Grid.RowSpan="2" Grid.Row="0" Style="{DynamicResource ItemDeleteButton}" />
</Grid>
</DataTemplate>
如何为我的 ListBoxItems 显示自定义错误模板?(我的目标是使项目的背景变成红色)