尽管 DataGridCell 中出现验证错误,如何继续工作?
How to continue working although a validation error in a DataGridCell occured?
我想在 DataGrid 中显示一些值。一列应显示整数值。当用户输入非数字字符时,我想告诉用户这一点,但这个值可能会保留下来。
目前我正在为 DataGridRow 使用 ValidationTemplate。问题是完整的行...完整的 DataGrid 不再是不可编辑的,直到在特定单元格中输入整数值。
我怎样才能实现它来通知用户他输入了错误的值但最终允许这样做?
这是我目前使用的样式:
<Style x:Key="errorRowStyle" TargetType="{x:Type DataGridRow}">
<Setter Property="ValidationErrorTemplate">
<Setter.Value>
<ControlTemplate>
<Grid>
<Ellipse Width="12" Height="12" Fill="Red" Stroke="Black" StrokeThickness="0.5"/>
<TextBlock FontWeight="Bold" Padding="4,0,0,0" Margin="0" VerticalAlignment="Top" Foreground="White" Text="!" />
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="Validation.HasError" Value="true">
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="BorderBrush" Value="Red"/>
<Setter Property="IsEnabled" Value="True" />
<Setter Property="ToolTip" Value="{Binding RelativeSource={x:Static RelativeSource.Self}, Path=(Validation.Errors)[0]}"/>
</Trigger>
</Style.Triggers>
</Style>
2018-01-13更新:
DataGrid: On cell validation error other row cells are uneditable/Readonly
很接近我的问题但是没有解决输入(无效)值不是的问题ObservableCollection
绑定(双向)到具有无效(字母数字)值的单元格(实际上是一个字符串 属性 但验证器针对整数值进行验证)但有效整数值是。 (正如我所提到的:GUI 是宽容而不是限制,应该给用户一个提示和可视化输入的值不符合要求,但即使是这个无效值也应该被接受。)
验证器可以是原因吗??
namespace ConfigTool.Tools
{
public class CycleValidationRule : ValidationRule
{
public override ValidationResult Validate(object value,
System.Globalization.CultureInfo cultureInfo)
{
//DataRowView dataRowView = (value as BindingGroup).Items[0] as DataRowView;
//string no = Convert.ToString(dataRowView.Row[0]);
if (int.TryParse(value.ToString(), out int i))
{
return new ValidationResult(true, null);
}
else
{
return new ValidationResult(false,
"Cycle should be an integer value.");
}
}
}
}
This
帮助我找到了解决方案。
我更改了我的 ValidationRule class 如下:
public class CycleValidationRule : ValidationRule
{
public override ValidationResult Validate(object value,
System.Globalization.CultureInfo cultureInfo)
{
BindingGroup group = (BindingGroup)value;
StringBuilder error = null;
foreach (var item in group.Items)
{
IDataErrorInfo info = item as IDataErrorInfo;
if (info != null)
{
if (!string.IsNullOrEmpty(info.Error))
{
if (error == null)
{
error = new StringBuilder();
}
error.Append((error.Length != 0 ? ", " : "") + info.Error);
}
}
}
if (error != null)
return new ValidationResult(false, error.ToString());
else
return new ValidationResult(true, "");
}
}
在底层实体中 class 以这种方式实现 IDataErrorInfo(提取):
string IDataErrorInfo.Error
{
get
{
StringBuilder error = new StringBuilder();
if (!int.TryParse(Cycle.ToString(), out int i))
{
error.Append("Cycle should be an integer value.");
}
return error.ToString();
}
}
在 XAML 文件中我添加了
<DataGrid.RowValidationRules>
<local:CycleValidationRule ValidationStep="UpdatedValue" />
</DataGrid.RowValidationRules>
到数据网格。
我想在 DataGrid 中显示一些值。一列应显示整数值。当用户输入非数字字符时,我想告诉用户这一点,但这个值可能会保留下来。 目前我正在为 DataGridRow 使用 ValidationTemplate。问题是完整的行...完整的 DataGrid 不再是不可编辑的,直到在特定单元格中输入整数值。 我怎样才能实现它来通知用户他输入了错误的值但最终允许这样做?
这是我目前使用的样式:
<Style x:Key="errorRowStyle" TargetType="{x:Type DataGridRow}">
<Setter Property="ValidationErrorTemplate">
<Setter.Value>
<ControlTemplate>
<Grid>
<Ellipse Width="12" Height="12" Fill="Red" Stroke="Black" StrokeThickness="0.5"/>
<TextBlock FontWeight="Bold" Padding="4,0,0,0" Margin="0" VerticalAlignment="Top" Foreground="White" Text="!" />
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="Validation.HasError" Value="true">
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="BorderBrush" Value="Red"/>
<Setter Property="IsEnabled" Value="True" />
<Setter Property="ToolTip" Value="{Binding RelativeSource={x:Static RelativeSource.Self}, Path=(Validation.Errors)[0]}"/>
</Trigger>
</Style.Triggers>
</Style>
2018-01-13更新:
DataGrid: On cell validation error other row cells are uneditable/Readonly
很接近我的问题但是没有解决输入(无效)值不是的问题ObservableCollection
绑定(双向)到具有无效(字母数字)值的单元格(实际上是一个字符串 属性 但验证器针对整数值进行验证)但有效整数值是。 (正如我所提到的:GUI 是宽容而不是限制,应该给用户一个提示和可视化输入的值不符合要求,但即使是这个无效值也应该被接受。)
验证器可以是原因吗??
namespace ConfigTool.Tools
{
public class CycleValidationRule : ValidationRule
{
public override ValidationResult Validate(object value,
System.Globalization.CultureInfo cultureInfo)
{
//DataRowView dataRowView = (value as BindingGroup).Items[0] as DataRowView;
//string no = Convert.ToString(dataRowView.Row[0]);
if (int.TryParse(value.ToString(), out int i))
{
return new ValidationResult(true, null);
}
else
{
return new ValidationResult(false,
"Cycle should be an integer value.");
}
}
}
}
This 帮助我找到了解决方案。
我更改了我的 ValidationRule class 如下:
public class CycleValidationRule : ValidationRule
{
public override ValidationResult Validate(object value,
System.Globalization.CultureInfo cultureInfo)
{
BindingGroup group = (BindingGroup)value;
StringBuilder error = null;
foreach (var item in group.Items)
{
IDataErrorInfo info = item as IDataErrorInfo;
if (info != null)
{
if (!string.IsNullOrEmpty(info.Error))
{
if (error == null)
{
error = new StringBuilder();
}
error.Append((error.Length != 0 ? ", " : "") + info.Error);
}
}
}
if (error != null)
return new ValidationResult(false, error.ToString());
else
return new ValidationResult(true, "");
}
}
在底层实体中 class 以这种方式实现 IDataErrorInfo(提取):
string IDataErrorInfo.Error
{
get
{
StringBuilder error = new StringBuilder();
if (!int.TryParse(Cycle.ToString(), out int i))
{
error.Append("Cycle should be an integer value.");
}
return error.ToString();
}
}
在 XAML 文件中我添加了
<DataGrid.RowValidationRules>
<local:CycleValidationRule ValidationStep="UpdatedValue" />
</DataGrid.RowValidationRules>
到数据网格。