【问题标题】:Validating a ListBoxItem rather than a ListBox INotifyDataErrorInfo验证 ListBoxItem 而不是 ListBox INotifyDataErrorInfo
【发布时间】:2020-02-24 00:46:26
【问题描述】:

我在这里尝试了解决方案: Validating a ListBoxItem rather than a ListBoxWPF ListBox ErrorTemplateWPF INotifyDataErrorInfo highlight ListBoxItem

我有一个列表框

<ListBox ItemsSource="{Binding ViewNewPanelsPairs}" ItemTemplate="{StaticResource LevelsTemplate}"  Style="{StaticResource PanelsListBox}"
                     SelectionMode="Single"  Margin="10" SelectedItem="{Binding CurrentViewNewPanelsPair, ValidatesOnNotifyDataErrors=True}" ItemContainerStyle="{StaticResource LevelItemTemplate}"/>

带有以下物品模板和itemcontainerstyle

<DataTemplate x:Key="LevelsTemplate" DataType="{x:Type VM:ViewNewPanelsPair}">
    <CheckBox VerticalContentAlignment="Center"  Content="{Binding View.Name}"   
              IsChecked="{Binding IsViewPanelsNotEmpty, Mode=OneWay}"
              IsHitTestVisible="False"
              IsEnabled="{Binding RelativeSource={RelativeSource Self}, Path=IsChecked}"/>
</DataTemplate>



<Style x:Key="LevelItemTemplate" 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>

和以下验证(实现 INotifyDataErrorInfo)

public ViewNewPanelsPair CurrentViewNewPanelsPair
    {
        get => currentViewNewPanelsPair;
        set
        {
            Set(() => CurrentViewNewPanelsPair, ref currentViewNewPanelsPair, value);
            ValidateProperty(nameof(CurrentViewNewPanelsPair));
        }
    }

protected void ValidateProperty(string PropertyName)
    {
        ClearErrors(PropertyName);
        switch (PropertyName)
        {
            case nameof(ViewNewPanelsPairs):
                if (!ViewNewPanelsPairs.Any(x => x.IsViewPanelsNotEmpty))
                {
                    AddError(PropertyName, "At least one view must have new panels");
                }
                break;
            case nameof(CurrentViewNewPanelsPair):
                if (CurrentViewNewPanelsPair.NewPanels.Count != 0 && CurrentViewNewPanelsPair.NewPanels.Count!=ViewNewPanelsPair.OldPanels.Count)
                {
                    AddError(PropertyName, "New Panels must be equal to Old Panels");
                }
                break;
        }
    }

无论我多么努力,我总是让整个列表框突出显示,并在错误时用红色边框包围(不是有错误的列表框项) 我的代码有问题吗??

注意:我调试了代码以确保确实存在错误并且确实存在并且它将它显示为整个列表框周围的边框

【问题讨论】:

  • inotifydataerrorinfo 实现应该在行视图模型中。你的在它的父母身上吗?
  • @Andy 如果我得到你所说的正确, inotifydataerrorinfo 的实现在 viewmodel 从它继承的基类中,但是我尝试将实现移动到 viewmodel 类本身并且也没有工作
  • 您正在将 itemssource 绑定到事物的集合。我所说的行视图模型是其中之一的类。不是视图模型公开了该集合。
  • @Andy,我现在知道了,不,我只是在 viewmodel 中实现了接口,那些东西是模型,我没有在其中实现接口,我会试试看
  • @Andy 我试过了,但是整个错误验证都失败了,我想我必须坚持在视图模型中实现

标签: wpf xaml listboxitem inotifydataerrorinfo


【解决方案1】:

当我在我的模型类“ViewNewPaanelsPair”上实现 INotifyDataErrorinfo 并将 itemscontrolcontainer 的数据触发器绑定到引发此类错误的属性时,它起作用了

<DataTrigger Binding="{Binding NewPanels}" > <Setter Property="BorderBrush" Value="Red"/> </DataTrigger>

感谢@Andy 的帮助

【讨论】:

    猜你喜欢
    • 2012-11-07
    • 1970-01-01
    • 1970-01-01
    • 2011-04-03
    • 1970-01-01
    • 2020-11-30
    • 2019-02-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多