WPF列表框错误模板 [英] WPF ListBox ErrorTemplate

查看:103
本文介绍了WPF列表框错误模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的WPF应用程序中,我有一个ListBox绑定到视图模型的集合. 这些视图模型通过实现INotifyDataErrorInfo支持验证. 我正在尝试在ListBox中显示包含验证错误的商品的错误模板.

In my WPF application I've a ListBox binding to a collection of view models. Those view models support validation by implement INotifyDataErrorInfo. I’m trying to display an error template for items with validation errors in my ListBox.

通过在ListBox的ItemSource绑定上设置 NotifyOnValidationError = True ,我可以使ListBox显示默认错误模板.
看起来像这样:

I’m able to get the ListBox to display the default error template by setting NotifyOnValidationError=True on the ItemSource binding of the ListBox.
Which looks like this:

我的列表框的代码:

<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项目模板:

The ListBox item template:

<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显示自定义错误模板? (我的目标是将项目的背景设为红色)

How can I display a custom error template for my ListBoxItems? (My goal is to make the background of the items red)

推荐答案

您也可以在ItemContainerStyle中的ListBoxItem上指定Valdiation.ErrorTemplate:

You can specify Valdiation.ErrorTemplate on ListBoxItem as well in ItemContainerStyle:

<ListBox>
    <ListBox.ItemContainerStyle>
        <Style TargetType="ListBoxItem">
            <Setter Property="Validation.ErrorTemplate">
                <Setter.Value>
                    <ControlTemplate>
                        <Border Background="Red" Opacity="0.2">
                            <AdornedElementPlaceholder/>
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </ListBox.ItemContainerStyle>
</ListBox>


当项目中的任何属性引发错误时,您很可能希望在ListBoxItem上显示它.说ListBoxItem数据中的Name属性无效,该属性绑定到TextBox.在文本框上将ValidatesOnDataErrors设置为true,并将Validation.ValidationAdornerSite设置为父级ListBoxItem.


Most likely you want to show it on ListBoxItem when any property in item throws an error. Say Name property in ListBoxItem data is invalid which is bind to TextBox. Set ValidatesOnDataErrors to true on textbox and set Validation.ValidationAdornerSite to parent ListBoxItem.

例如:

<ListBox>
    <ListBox.ItemTemplate>
        <DataTemplate>
            <TextBox Text="{Binding Name, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}"
                     Validation.ValidationAdornerSite="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ListBoxItem}}"/>
        </DataTemplate>
    </ListBox.ItemTemplate>
    <ListBox.ItemContainerStyle>
        <Style TargetType="ListBoxItem">
            <Setter Property="Validation.ErrorTemplate">
                <Setter.Value>
                    <ControlTemplate>
                        <Border Background="Red" Opacity="0.2">
                           <AdornedElementPlaceholder/>
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </ListBox.ItemContainerStyle>
</ListBox>

这篇关于WPF列表框错误模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆