相关疑难解决方法(0)

为什么WPF Style在ToolTip中显示验证错误为TextBox工作但对ComboBox失败?

我使用一个典型的样式来显示验证错误作为IErrorDataInfo的工具提示,如下所示,它可以正常工作.

    <Style TargetType="{x:Type TextBox}">
        <Style.Triggers>
            <Trigger Property="Validation.HasError" Value="true">
                <Setter Property="ToolTip"
                Value="{Binding RelativeSource={RelativeSource Self},
            Path=(Validation.Errors)[0].ErrorContent}"/>
            </Trigger>
        </Style.Triggers>
    </Style>
Run Code Online (Sandbox Code Playgroud)

但是当我尝试为这样的ComboBox做同样的事情时,它失败了

    <Style TargetType="{x:Type ComboBox}">
        <Style.Triggers>
            <Trigger Property="Validation.HasError" Value="true">
                <Setter Property="ToolTip"
                Value="{Binding RelativeSource={RelativeSource Self},
            Path=(Validation.Errors)[0].ErrorContent}"/>
            </Trigger>
        </Style.Triggers>
    </Style>
Run Code Online (Sandbox Code Playgroud)

我在输出窗口中得到的错误是:

System.Windows.Data错误:17:无法从'(Validation.Errors)'获取'Item []'值(类型'ValidationError')(类型'ReadOnlyObservableCollection`1').BindingExpression:路径=(0)[0] .ErrorContent; DataItem ='ComboBox'(Name ='ownerComboBox'); target元素是'ComboBox'(Name ='ownerComboBox'); target属性是'ToolTip'(类型'Object')ArgumentOutOfRangeException:'System.ArgumentOutOfRangeException:指定的参数超出了有效值的范围.参数名称:index'

奇怪的是,如果我更改任何ComboBox值,它也会尝试在关闭窗口时进行无效的数据库更改(这也是发生绑定错误时)!

无法将值NULL插入列'EmpFirstName',表'OITaskManager.dbo.Employees'; 列不允许空值.INSERT失败.该语句已终止.

简单地通过评论风格完美的每一个作品.我该如何解决?

万一有人需要它,其中一个comboBox'xaml如下:

<ComboBox ItemsSource="{Binding Path=Employees}" 
                  SelectedValuePath="EmpID"                       
                  SelectedValue="{Binding Path=SelectedIssue.Employee2.EmpID,
                     Mode=OneWay, ValidatesOnDataErrors=True}" 
                  ItemTemplate="{StaticResource LastNameFirstComboBoxTemplate}"
                  Height="28" Name="ownerComboBox" Width="120" Margin="2" 
                  SelectionChanged="ownerComboBox_SelectionChanged" />


<DataTemplate x:Key="LastNameFirstComboBoxTemplate">
    <TextBlock> 
         <TextBlock.Text> 
             <MultiBinding StringFormat="{}{1}, {0}" > 
                   <Binding Path="EmpFirstName" /> 
                   <Binding Path="EmpLastName" /> 
             </MultiBinding> …
Run Code Online (Sandbox Code Playgroud)

c# data-binding validation wpf styles

38
推荐指数
2
解决办法
3万
查看次数

为什么AdornedElement.(Validation.Errors).CurrentItem.ErrorContent now(VS2017 15.4)导致intellisense错误?

我一直在使用以下错误装饰模板很长一段时间了:

<ControlTemplate x:Key="ErrorAdornerTemplateStyle" TargetType="{x:Type Control}">
    <Grid ClipToBounds="False" >
        <Border BorderBrush="Red" BorderThickness="2" Margin="-1" 
         ToolTip="{Binding ElementName=adornedElement, Path=AdornedElement.(Validation.Errors).CurrentItem.ErrorContent }">
            <AdornedElementPlaceholder Name="adornedElement" />
        </Border>
        <Polygon Points="15,15 15,0 0,0"
                 Fill="Red"
                 HorizontalAlignment="Right"
                 VerticalAlignment="Top" 
                 ToolTip="{Binding ElementName=adornedElement, Path=AdornedElement.(Validation.Errors).CurrentItem.ErrorContent }"/>
    </Grid>
</ControlTemplate>
Run Code Online (Sandbox Code Playgroud)

...它在运行时工作得很好(据我所知).

但是,在过去一个月对VS和WPF以及NET Standard 2进行了一系列升级之后,我注意到我的syles xaml文件中的intellisense给出了以下CurrentItem标识错误:

在'ReadOnlyObservableCollection'类型中找不到属性'CurrentItem'.

这只是一个令人讨厌的VS错误,还是VS警告我需要适应的WPF子系统中的某种变化?

wpf xaml visual-studio

9
推荐指数
1
解决办法
1075
查看次数

标签 统计

wpf ×2

c# ×1

data-binding ×1

styles ×1

validation ×1

visual-studio ×1

xaml ×1