如何绑定一个简单的字符串值到一个文本框?绑定、文本框、简单、字符串值

由网友(坐在情敌坟头唱忐忑)分享简介:我使用WPF。我想一个文本框绑定在xaml.cs类初始化一个简单的字符串类型的值。该文本框没有显示任何东西。这是我的XAML code:<文本框Grid.Column =1WIDTH =387的Horizo​​ntalAlignment =左Grid.ColumnSpan =2文本={绑定路径=名称2}/ &GT...

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

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

和C#code是这样的:

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

解决方案 程序运行后,在文本框TEXT1中输入一个字符串,单击命令按纽,把字符串中偶数位置上的字符输入到文本框TEXT2

您从未设置你的属性值。简单地定义设置{_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));
        }
    }
}

阅读全文

相关推荐

最新文章