2

我有一个名为 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# 代码如何实现它?

4

2 回答 2

1

为 TextBoxColumn 控件命名并尝试按元素名称绑定它的属性

<TextBox Text="{Binding ElementName=txtBoxCol, Path=FieldName}"></TextBox>
于 2012-09-06T08:11:23.337 回答
0

没有代码你不能做事,没有办法绑定绑定的属性(在这种情况下,你想Binding.Path成为任何东西FieldName)。

于 2012-09-06T08:09:58.977 回答