5

私の 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 のカスタム エラー テンプレートを表示するにはどうすればよいですか? (私の目標は、アイテムの背景を赤くすることです)

4

1 に答える 1