【发布时间】:2013-04-01 03:36:16
【问题描述】:
以下是我正在使用的示例代码。我为TextBox 创建了名为ErrorMessageServce.ErrorMessage 的附加属性。每当ValidationError 被填充时,它将调用ErrorMessageService 的属性更改事件。
从那里我想要的是,如果出现错误,我想突出显示该特定单元格。所以我想在ErrorMessageServicePropertyChanged 做,但我得到TextBox 对象。
所以问题是:
1) 如何从该文本框对象中获取 Datagridcell;
或:
2) 如何突出显示该特定单元格;
3) 如何在编辑模式下显示特定单元格(即应该显示文本框)
XAML:
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Grid.Column="1"
Width="150" Height="25">
</TextBlock>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<TextBox Grid.Column="1" Style="{DynamicResource ValidatingTextBox}"
x:Name="NameText" Text="{Binding CompanyName,ValidatesOnDataErrors=True,ValidatesOnExceptions=True}" App:ErrorMessageService.ErrorMessage="{Binding ValidationResult,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Width="150" Height="25">
</TextBox>
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>
ErrorMessageService
public static class ErrorMessageService
{
public static readonly DependencyProperty ValidationErrorProperty =
DependencyProperty.RegisterAttached("ErrorMessage", typeof(ValidationResult), typeof(ErrorMessageService),
new FrameworkPropertyMetadata(default(ValidationResult), ErrorMessageServicePropertyChanged));
public static ValidationResult GetErrorMessage(Control control)
{
return (ValidationResult)control.GetValue(ValidationErrorProperty);
}
public static void SetErrorMessage(Control control, object value)
{
control.SetValue(ValidationErrorProperty, value);
}
private static void ErrorMessageServicePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
//here i am getting d as textbox , from this how to get datagridcell object , so that i can highlight
}
}
谢谢。
【问题讨论】:
标签: c# wpf validation datagrid datatemplate