如何将文本框文本绑定到xaml中的属性值 [英] How to Bind TextBox Text to Value of Property in xaml

查看:213
本文介绍了如何将文本框文本绑定到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


这篇关于如何将文本框文本绑定到xaml中的属性值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆