【问题标题】:How to bind a simple string value to a text box?如何将简单的字符串值绑定到文本框?
【发布时间】:2013-07-20 16:38:51
【问题描述】:

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

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

C# 代码是这样的:

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

【问题讨论】:

  • 你能发布你的 XAML 吗?
  • @Coder 我发布了它。见第一行代码。
  • 你认为你在哪里设置值?
  • 我想通过 binding 在文本框中显示值。 @罗兰肖。为此,我写了 Text="{Binding Path=Name2}".... 我做错了吗?
  • 你需要实现INotifyPropertyChanged

标签: c# .net wpf xaml


【解决方案1】:

您永远不会设置您的财产的价值。简单地定义set { _name2 = "abcdef"; } 并不会真正设置属性的值,直到您实际执行设置操作。

您可以将代码更改为如下所示:

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

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

另外,正如人们所提到的,如果您打算稍后修改属性的值并希望 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));
        }
    }
}

【讨论】:

  • 抱歉这么晚才回复,但是表单控件是如何订阅PropertyChangedEventHandler的呢?
【解决方案2】:

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

DataContext = this;

【讨论】:

    【解决方案3】:

    你为什么不添加一个视图模型并将你的属性保留在那里?

    查看模型类

    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 用户控件

    public partial class EntitiesView : UserControl
    {
    
        public EntitiesView()
        {
            InitializeComponent();
            this.DataContext = new TestViewModel();
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2012-04-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-07
      • 2020-01-26
      • 1970-01-01
      相关资源
      最近更新 更多