Ниже приведен пример кода, который я использую. Я создал прикрепленное свойство для TextBox
под названием ErrorMessageServce.ErrorMessage
. Всякий раз, когда заполняется ValidationError
, он вызывает событие изменения свойства ErrorMessageService
.Как получить объект ячейки datagrid из поля его шаблона wpf
Оттуда, что я хочу, я хочу выделить эту конкретную ячейку, если произошла ошибка. поэтому я думал сделать в ErrorMessageServicePropertyChanged
, но там я получаю TextBox
объект.
Так вопрос:
1) Как получить Datagridcell из этого текстового объекта;
или:
2) Как higlight этой конкретной ячейки;
3) Как отобразить эту конкретную ячейку в режим редактирования (т.е. .. Textbox должен отображаться)
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
}
}
Спасибо.