Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/271.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# ItemsControl验证样式在添加/删除项时不更新_C#_Wpf_Observablecollection_Itemscontrol_Idataerrorinfo - Fatal编程技术网

C# ItemsControl验证样式在添加/删除项时不更新

C# ItemsControl验证样式在添加/删除项时不更新,c#,wpf,observablecollection,itemscontrol,idataerrorinfo,C#,Wpf,Observablecollection,Itemscontrol,Idataerrorinfo,我有一个ItemsControl,它绑定到一个observedcollection。这个集合有一个验证规则(使用IDataErrorInfo),基本上是说“如果列表中有一个‘报价’,那么也必须有一个‘客户’ 我可以看到验证规则正在按预期工作,因为我有一个绑定到命令的按钮,如果出现错误,该按钮将无法执行。但是,ItemsControl不会以错误样式进行可视化更新 这种错误样式确实有效。我在许多其他控件上使用它(主要是TextBox和ComboBox),如果绑定的集合设置为新集合,它确实会显示在It

我有一个
ItemsControl
,它绑定到一个
observedcollection
。这个集合有一个验证规则(使用
IDataErrorInfo
),基本上是说“如果列表中有一个‘报价’,那么也必须有一个‘客户’

我可以看到验证规则正在按预期工作,因为我有一个绑定到命令的按钮,如果出现错误,该按钮将无法执行。但是,
ItemsControl
不会以错误样式进行可视化更新

这种错误样式确实有效。我在许多其他控件上使用它(主要是
TextBox
ComboBox
),如果绑定的集合设置为新集合,它确实会显示在
ItemsControl
上。错误样式仅在添加和删除时才会更新

<ItemsControl Margin="5" ItemsSource="{e:Binding Path=CurrentBooking.Regardings}" Background="Transparent">
    <!-- Items Panel and ItemTemplate Stuff -->
</ItemsControl>

虽然这不是一个解决方案,但它可以避免问题(如果可能的话)。为什么不提示用户同时输入报价和用户(通过类似弹出窗口的方式)但是用户可以将客户从集合中删除,我不想阻止这一点,因为他们可能希望将其更改为其他客户。
<Style x:Key="ErrorStyle" TargetType="FrameworkElement">
    <Style.Triggers>
        <Trigger Property="Validation.HasError" Value="true">
            <Setter Property="ToolTip" Value="{Binding RelativeSource={x:Static RelativeSource.Self},
             Path=(Validation.Errors).CurrentItem.ErrorContent}"/>
        </Trigger>
        <!--Clear Template if false-->
        <Trigger Property="Validation.HasError" Value="false">
            <Setter Property="Validation.ErrorTemplate" Value="{x:Null}"/>
        </Trigger>
    </Style.Triggers>
</Style>

<Style TargetType="ItemsControl" BasedOn="{StaticResource ErrorStyle}">
    <Setter Property="Background" Value="Transparent"/>
</Style>