3

我正在使用 DataSet 填充 DataGrid。我通过在每个 DataGridColumn 中的 make 为验证规则启用了我的 dataGrid,ValidatesOnDataError=True但是当我添加一个DataGridRow具有 DataErrors 的时,DataGridRow它被阻止,我不能做任何其他事情,也不能更正DataGridRow

     <!--Template for DataGridRow in Error state-->        
                <ControlTemplate x:Key="BasicRowValidationErrorTemplate">
                    <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"  />
                    </Grid>
                </ControlTemplate>

     <!--Style for DataGridCell in static mode-->
            <Style x:Key="textBlockInError" TargetType="{x:Type TextBlock}">
                <Style.Triggers>
                    <Trigger Property="Validation.HasError" Value="true">
                        <Setter Property="ToolTip"
                  Value="{Binding RelativeSource={x:Static RelativeSource.Self},
                  Path=(Validation.Errors)[0].ErrorContent}"/>
                    </Trigger>
                </Style.Triggers>
            </Style>

    <!--Style for DataGridCell in edit mode-->
            <Style x:Key="textBoxInError" TargetType="{x:Type TextBox}">
                <Style.Triggers>
                    <Trigger Property="Validation.HasError" Value="true">
                        <Setter Property="Background" Value="Red"/>
                        <Setter Property="ToolTip"
                  Value="{Binding RelativeSource={x:Static RelativeSource.Self},
                  Path=(Validation.Errors)[0].ErrorContent}"/>
                    </Trigger>
                </Style.Triggers>
            </Style>

我已经定义了我的列

<DataGrid>
   <DataGrid.RowValidationRules>
      <DataErrorValidationRule ValidationStep="ComittedValue"/>
   </DataGrid.RowValidationRules>
   <DataGrid.Columns>
    <DataGridTextColumn Header="Nombre" Binding="{Binding Nombre, UpdateSourceTrigger=PropertyChanged,
                        ValidatesOnDataErrors=True}" ElementStyle="{StaticResource textBlockInError}" 
                                        EditingElementStyle="{StaticResource textBoxInError}"/>    
    <DataGridTemplateColumn Header="Price">
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding Price, ValidatesOnDataErrors=True}" Style="{StaticResource textBlockInError}"/>
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                        <DataGridTemplateColumn.CellEditingTemplate>
                            <DataTemplate>
                                <ComboBox ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor,
                                    AncestorType={x:Type UserControl},AncestorLevel=2},
                                    Path=DataContext.Prices}" DisplayMemberPath="DisplayMember" 
                                    SelectedValue="{Binding Price}" IsEditable="{Binding RelativeSource={RelativeSource FindAncestor, 
                                            AncestorType={x:Type UserControl}, AncestorLevel=2},
                                            Path=DataContext.IsComboBoxEditable, Mode=OneWay}"/>
                            </DataTemplate>
                        </DataGridTemplateColumn.CellEditingTemplate>
                    </DataGridTemplateColumn>
   </DataGrid.Columns>
</DataGrid>

我的 ViewModel 中有

DataTable Price;
DataTable Prices;

Price 有两列 Name 和 Price,Prices 有两列 DisplayMember 和 ValueMember

我从文件中获取 Precios,如果未加载文件,则我没有要显示的 precies,因此当我没有文件时,我将我的组合框设为 Editable。只需将我选择的值设置为用户编写的值

private void ComboBox_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
{
    ComboBox comboBox = sender as ComboBox;
    if (comboBox.IsEditable)
    {
        comboBox.SelectedValue = (sender as ComboBox).Text;
    }
}

为什么阻止我的 DataGridRow ?

4

0 回答 0