如何将文本框文本绑定到xaml中的属性值

如何将文本框文本绑定到xaml中的属性值

问题描述:

我有一个名为TextBoxColumn的自定义类,如下所示

I have a custom class called TextBoxColumn as Follows

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列时:


When creating DataGrid columns from XAML:

<DataGrid>
    <DataGrid.Columns>
        <local:TextBoxControl FieldName="FirstName"/>
        <local:TextBoxControl FieldName="LastName"/>
    </DataGrid.Columns>
</DataGrid>


在XAML词典中,我为此TextBoxColumn定义了单元格模板:


In XAML Dictionary, I have defined the Cell Template for this TextBoxColumn:

<DataTemplate x:Key="TextBoxColumn_CellTemplate">
    <TextBox Text="{Binding FieldName}"/> <!-- Here is the problem -->
</DataTemplate>


如何获取TextBoxColumn的FieldName属性的值并将其绑定到Text属性?


How to get the value of FieldName property of TextBoxColumn and Bind it to Text property? How can I achieve it without C# code?

hiii,
只需放置此代码
TextBoxColumn obj = new TextBoxColumn()
textbox1.DataContext = obj;

或阅读这篇文章对您有帮助
hiii,
just place this code
TextBoxColumn obj=new TextBoxColumn()
textbox1.DataContext=obj;

or read this article will help you