我有一个名为 TextBoxColumn 的自定义类,如下所示
public class TextBoxColumn : DataGridTemplateColumn
{
public static readonly DependencyProperty FieldNameProperty = DependencyProperty.Register("FieldName", typeof(string), typeof(TextBoxColumn), new PropertyMetadata(""));
public string FieldName
{
get { return (string)GetValue(FieldNameProperty); }
set { SetValue(FieldNameProperty, value); }
}
}
从 XAML 创建 DataGrid 列时:
<DataGrid>
<DataGrid.Columns>
<local:TextBoxControl FieldName="FirstName"/>
<local:TextBoxControl FieldName="LastName"/>
</DataGrid.Columns>
</DataGrid>
在 XAML 字典中,我为此 TextBoxColumn 定义了单元格模板:
<DataTemplate x:Key="TextBoxColumn_CellTemplate">
<TextBox Text="{Binding FieldName}"/> <!-- Here is the problem, if I give FirstName instead of FieldName, it works fine -->
</DataTemplate>`
如何获取 TextBoxColumn 的 FieldName 属性的值并将其绑定到 Text 属性?没有 C# 代码如何实现它?