Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/28.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
WPF数据绑定不起作用_Wpf_Data Binding_Textbox - Fatal编程技术网

WPF数据绑定不起作用

WPF数据绑定不起作用,wpf,data-binding,textbox,Wpf,Data Binding,Textbox,这是我从字符串(History.current_commad)到文本框(tbCommand)的数据绑定: history.current_命令正在更改,但文本框未更新。怎么了 谢谢您之所以看不到文本块中反映的更改是因为当前_命令只是一个字段,因此绑定不知道它是何时被执行的 解决此问题的最简单方法是让历史类实现INotifyPropertyChanged,将当前_命令转换为属性,然后在属性的setter中引发PropertyChanged事件: public class History : INo

这是我从字符串(History.current_commad)到文本框(tbCommand)的数据绑定:

history.current_命令正在更改,但文本框未更新。怎么了


谢谢

您之所以看不到
文本块中反映的更改
是因为
当前_命令
只是一个字段,因此
绑定
不知道它是何时被执行的

解决此问题的最简单方法是让
历史
类实现
INotifyPropertyChanged
,将
当前_命令
转换为属性,然后在属性的setter中引发
PropertyChanged
事件:

public class History : INotifyPropertyChanged
{

    public event PropertyChangedEventHandler PropertyChanged;

    private void NotifyPropertyChanged(string propName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propName));
        }
    }

    private string _current_command;
    public string current_command
    {
        get
        {
            return _current_command;
        }
        set
        {
            if (_current_command == null || !_current_command.Equals(value))
            {
                // Change the value and notify that the property has changed
                _current_command = value;
                NotifyPropertyChanged("current_command");
            }
        } 
    }
}
现在,当您为
当前_命令
赋值时,事件将触发,
绑定
也将知道更新其目标


如果您发现自己有很多类要绑定到它们的属性,则应该考虑将事件和帮助器方法移到基类中,这样就不会重复编写相同的代码。

<代码>历史类看起来是什么?公共类历史{Prpublic String Currnth-Cub命令;} Hi@ DLV,你能看看这个问题吗?我在代码中进行数据绑定的原因是我无法正确地使用xmlns。谢谢
public class History : INotifyPropertyChanged
{

    public event PropertyChangedEventHandler PropertyChanged;

    private void NotifyPropertyChanged(string propName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propName));
        }
    }

    private string _current_command;
    public string current_command
    {
        get
        {
            return _current_command;
        }
        set
        {
            if (_current_command == null || !_current_command.Equals(value))
            {
                // Change the value and notify that the property has changed
                _current_command = value;
                NotifyPropertyChanged("current_command");
            }
        } 
    }
}