小编And*_*ill的帖子

禁用基于DataGrid DataGridTextColumn验证失败的按钮

我正在使用System.Windows.Controls.DataGrid(WPF,.NET 4.0,C#).如果单元格的验证失败(HasErrors == TRUE),则"确定"按钮应为灰色(IsEnabled = FALSE).

DataGrid验证使用ValidationRule执行.

我在StackOverflow上阅读了几篇密切相关的文章,但我仍然被卡住了.我认为问题是验证是在DataGridRow上,但是OK按钮的IsEnabled绑定正在查看整个网格.

要查看错误,请在网格中添加第三行,并输入无效数字(例如200).或者只是将两个库存值中的一个编辑为无效(小于0,非整数或大于100) ).

这是xaml:

<Window x:Class="SimpleDataGridValidation.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:c="clr-namespace:SimpleDataGridValidation"
        Title="MainWindow"
        Height="350"
        Width="525">
  <Window.Resources>
    <c:BoolToOppositeBoolConverter x:Key="boolToOppositeBoolConverter" />
  </Window.Resources>

  <Grid>
    <Grid.RowDefinitions>
      <RowDefinition Height="*"></RowDefinition>
      <RowDefinition Height="Auto"></RowDefinition>
    </Grid.RowDefinitions>

    <DataGrid Name="myDataGrid"
              ItemsSource="{Binding}"
              AutoGenerateColumns="False">

      <DataGrid.Columns>
        <DataGridTextColumn Header="Time"
                            x:Name="TimeField">
          <DataGridTextColumn.Binding>
            <Binding Path="Time"
                     UpdateSourceTrigger="PropertyChanged">
              <Binding.ValidationRules>
                <c:TimeValidationRule />
              </Binding.ValidationRules>
            </Binding>
          </DataGridTextColumn.Binding>
        </DataGridTextColumn>
      </DataGrid.Columns>

      <DataGrid.RowValidationErrorTemplate>
        <ControlTemplate>
          <Grid Margin="0,-2,0,-2"
                ToolTip="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGridRow}},
                             Path=(Validation.Errors)[0].ErrorContent}">
            <Ellipse StrokeThickness="0"
                     Fill="Red"
                     Width="{TemplateBinding FontSize}"
                     Height="{TemplateBinding FontSize}" />
            <TextBlock Text="!"
                       FontSize="{TemplateBinding FontSize}"
                       FontWeight="Bold"
                       Foreground="White"
                       HorizontalAlignment="Center"
                       VerticalAlignment="Center" />
          </Grid>
        </ControlTemplate> …
Run Code Online (Sandbox Code Playgroud)

c# validation wpf xaml datagrid

3
推荐指数
1
解决办法
2390
查看次数

标签 统计

c# ×1

datagrid ×1

validation ×1

wpf ×1

xaml ×1