【问题标题】:WPF DataGrid Validation error not being caught未捕获 WPF DataGrid 验证错误
【发布时间】:2009-12-16 16:28:11
【问题描述】:

我正在尝试对 DataGrid 中的单元格使用异常验证以及 DataGridTextColumn 的 EditingElementStyle 上的样式来设置带有错误内容的工具提示。发生错误但未在 WPF 中捕获或显示。

代码和异常如下所示。谁能告诉我我需要解决这个问题吗?

干杯,
浆果

这是一个例外:

System.Windows.Data Error: 8 : Cannot save value from target back to source. 
BindingExpression:Path=Allocations[6].Amount; DataItem='ActivityViewModel' (HashCode=-938045583); 
target element is 'TextBox' (Name=''); 
target property is 'Text' (type 'String') 
TargetInvocationException:'System.Reflection.TargetInvocationException: 
Exception has been thrown by the target of an invocation. ---> 
Domain.Core.PreconditionException: An allocation must be less than one day.

这是 DataGridTextColumn 的 xaml:

<dg:DataGridTextColumn 
    ....                
    EditingElementStyle="{StaticResource cellEditStyle}"
    Binding="{Binding Allocations[6].Amount, Converter={StaticResource amtConv}, 
        ValidatesOnExceptions=True}"
                               />

下面是应该提供错误提示反馈的样式:

    <Style x:Key="cellEditStyle" TargetType="{x:Type TextBox}">
    <Setter Property="BorderThickness" Value="0"/>
    <Setter Property="Padding" Value="0"/>
    <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>

【问题讨论】:

    标签: wpf validation datagrid


    【解决方案1】:

    可能有点晚了,但由于我遇到了同样的问题,这里有一个解决方法供进一步参考(使用 .NET 4.0.30319 测试)。

    1) 捕捉异常

    虽然原始帖子中的以下绑定代码适用于 TextBox,例如,它不适用于 DataGrid 文本单元格(即使 Msn 文档说明如此):

    <!-- Doesn't work -->
    <DataGridTextColumn Binding="{Binding Path=Age, ValidatesOnExceptions=True}"
                        ...
                        />
    

    你必须添加这个位:

    <DataGridTextColumn Binding="{Binding Path=Age, Mode=TwoWay, ValidatesOnExceptions=True}"
                        ...
                        />
    

    请注意,奇怪的是(无论如何对我来说),异常将被捕获并在行标题中以感叹号显示。没有Mode=TwoWay 部分,您将没有红色边框,也无法应用样式。

    2) 应用样式

    另一个困难是设置样式以防出错,因为一旦您开始验证过程,编辑元素就会关闭。所以附加一个样式:

    <!-- Doesn't work -->
    <DataGridTextColumn Binding="{Binding Path=Age, Mode=TwoWay, ValidatesOnExceptions=True}"
                        EditingElementStyle="{StaticResource datagridTBStyle}"
                        ...
                        />
    

    如果您想触发验证错误,则根本无法工作。与 CellStyle 类似,它不会触发错误标志。您必须使用一个技巧并声明一个 FrameworkElement 样式,如下所示:

    <DataGridTextColumn Binding="{Binding Path=Age, Mode=TwoWay, ValidatesOnExceptions=True}"
                        ElementStyle="{StaticResource datagridElemStyle}"
                        ...
                        />
    

    好消息是您可以在派生元素(如 TextBlock)上定义样式,并从它们的属性中受益:

    <Style x:Key="datagridElemStyle" TargetType="{x:Type TextBlock}">
      <Style.Triggers>
        <Trigger Property="Validation.HasError" Value="True">
          <Setter Property="Background" Value="Yellow" />
          <Setter Property="ToolTip" 
                  Value="{Binding RelativeSource={RelativeSource Self}, Path=(Validation.Errors)[0].ErrorContent}"/>
        </Trigger>
      </Style.Triggers>
    </Style>
    

    【讨论】:

      猜你喜欢
      • 2011-07-03
      • 2011-06-29
      • 1970-01-01
      • 2015-10-04
      • 2015-08-06
      • 2014-08-08
      • 2013-11-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多