0
下面是我正在使用的示例代碼。我爲TextBox
創建了附加屬性,名爲ErrorMessageServce.ErrorMessage
。無論何時填入ValidationError
,它都會調用ErrorMessageService
的財產變更事件。如何從其模板字段獲取datagrid單元格對象wpf
從那裏我想要的是,如果出現錯誤,我想突出顯示特定的單元格。所以我想在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
}
}
感謝。