如何绑定一个简单的字符串值到一个文本框? [英] How to bind a simple string value to a text box?

查看:193
本文介绍了如何绑定一个简单的字符串值到一个文本框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用WPF。我想一个文本框绑定在xaml.cs类初始化一个简单的字符串类型的值。该文本框没有显示任何东西。这是我的XAML code:

 <文本框Grid.Column =1WIDTH =387的Horizo​​ntalAlignment =左Grid.ColumnSpan =2文本={绑定路径=名称2}/ >
 

和C#code是这样的:

 公共部分类EntitiesView:用户控件
{
    私人字符串_name2;
    公共字符串名称2
    {
        {返回_name2; }
        集合{_name2ABCDEF=; }
    }
    公共EntitiesView()
    {
        的InitializeComponent();
    }
}
 

解决方案

您从未设置你的属性值。简单地定义设置{_name2 =ABCDEF; } 实际上并没有设置属性的值,直到实际执行的设置操作。

您可以更改您的code看起来像这样为它工作:

 公共部分类EntitiesView:用户控件
{
    私人字符串_name2;
    公共字符串名称2
    {
        {返回_name2; }
        集合{_name2 =价值; }
    }

    公共EntitiesView()
    {
        名称2 =ABCDEF;
        的DataContext =这一点;
        的InitializeComponent();
    }
}
 

此外,人们所提到的,如果你打算修改你的财产的价值以后,想用户界面,以反映它,你将需要实施 INotifyPropertyChanged的接口

 公共部分类EntitiesView:用户控件,INotifyPropertyChanged的
{
    私人字符串_name2;
    公共字符串名称2
    {
        {返回_name2; }
        组
        {
            _name2 =价值;
            RaisePropertyChanged(名称2);
        }
    }

    公共EntitiesView()
    {
        名称2 =ABCDEF;
        的DataContext =这一点;
        的InitializeComponent();
    }

    公共事件PropertyChangedEventHandler的PropertyChanged;
    保护无效RaisePropertyChanged(字符串propertyName的)
    {
        VAR处理器=的PropertyChanged;
        如果(处理!= NULL)
        {
            处理程序(这一点,新PropertyChangedEventArgs(propertyName的));
        }
    }
}
 

I am using wpf. I want to bind a textbox with a simple string type value initialized in xaml.cs class. The TextBox isn't showing anything. Here is my XAML code:

<TextBox Grid.Column="1" Width="387" HorizontalAlignment="Left" Grid.ColumnSpan="2" Text="{Binding Path=Name2}"/>

And the C# code is this:

public partial class EntitiesView : UserControl
{
    private string _name2;
    public string Name2
    {
        get { return _name2; }
        set { _name2 = "abcdef"; }
    }
    public EntitiesView()
    {
        InitializeComponent();
    }
}

解决方案

You never set the value of your property. Simply defining set { _name2 = "abcdef"; } does not actually set the value of your property until you actually perform the set operation.

You can change your code to look like this for it to work:

public partial class EntitiesView : UserControl
{
    private string _name2;
    public string Name2
    {
        get { return _name2; }
        set { _name2 = value; }
    }

    public EntitiesView()
    {
        Name2 = "abcdef";
        DataContext = this;
        InitializeComponent();
    }
}

Also, as people have mentioned, if you intend to modify your property's value later on and want the UI to reflect it, you'll need to implement the INotifyPropertyChanged interface:

public partial class EntitiesView : UserControl, INotifyPropertyChanged
{
    private string _name2;
    public string Name2
    {
        get { return _name2; }
        set
        {
            _name2 = value;
            RaisePropertyChanged("Name2");
        }
    }

    public EntitiesView()
    {
        Name2 = "abcdef";
        DataContext = this;
        InitializeComponent();
    }

    public event PropertyChangedEventHandler PropertyChanged;
    protected void RaisePropertyChanged(string propertyName)
    {
        var handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

这篇关于如何绑定一个简单的字符串值到一个文本框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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