wpf 如何将简单的字符串值绑定到文本框?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17764323/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 09:15:10  来源:igfitidea点击:

How to bind a simple string value to a text box?

c#.netwpfxaml

提问by Dua Ali

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

我正在使用 wpf。我想用在 xaml.cs 类中初始化的简单字符串类型值绑定一个文本框。在TextBox不显示任何。这是我的 XAML 代码:

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

And the C# code is this:

而 C# 代码是这样的:

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

回答by Adi Lester

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.

你从来没有设定你的财产的价值。set { _name2 = "abcdef"; }在您实际执行设置操作之前,简单定义并不会实际设置您的属性的值。

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 INotifyPropertyChangedinterface:

此外,正如人们所提到的,如果您打算稍后修改属性的值并希望 UI 反映它,则需要实现该INotifyPropertyChanged接口:

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));
        }
    }
}

回答by UsmanAzam

Just add this line in your EntitiesViewconstructor

只需在您的EntitiesView构造函数中添加这一行

DataContext = this;

回答by Kurubaran

Why dont you add a view model and keep your property there ?

为什么不添加视图模型并将您的财产保留在那里?

View Model class

查看模型类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;

namespace WpfApplication1
{
    public class TestViewModel : INotifyPropertyChanged
    {
        public string _name2;
        public string Name2
        {
            get { return "_name2"; }
            set
            { 
                _name2 = value;
                OnPropertyChanged(new PropertyChangedEventArgs("Name2"));
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        public void OnPropertyChanged(PropertyChangedEventArgs e)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, e);
            }
        }
    }
}

EntitiesView User Control

EntityView 用户控件

public partial class EntitiesView : UserControl
{

    public EntitiesView()
    {
        InitializeComponent();
        this.DataContext = new TestViewModel();
    }
}