我已经挠头2天了。虽然我是 .NET 的新手,但我已经阅读了 20 多个帖子和问题,我认为我的代码应该可以工作。有些请投光。
XAML:
<TextBox Grid.Column="3" Name="testgrid" Text="{Binding textsource, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}"></TextBox>
后面的代码:
public MainWindow()
{
InitializeComponent();
textbind tb = new textbind();
tb.textsource = "one"; //one is displayed in the textbox.
testgrid.DataContext = tb;
}
和:
public class textbind : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(String info)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
private string a=string.Empty;
public textbind()
{
}
public string textsource
{
get { return a; }
set
{
if (value != a)
{
a = value;
NotifyPropertyChanged(textsource);
}
}
}
}
改变属性:
public class changevalue
{
//code doing things. this class is initialized by some other processes.
textbind tb1 = new textbind();
tb1.textsource = "two"; // no updates to two in the text box.
}
我相信每次我更改 textsource 属性时,它都会反映文本框中的更改,但它不会发生。索内恩请帮忙。
谢谢。