我在.cs中添加代码
public static readonly DependencyProperty lbStatusProperty =
DependencyProperty.Register("lbStatus", typeof(string), typeof(SingleTalkView),
new PropertyMetadata(""));
public string lbStatus
{
get { return (string)GetValue(lbStatusProperty); }
set { SetValue(lbStatusProperty, value); }
}在xaml中
<TextBlock Text="{Binding lbStatus}" Style="{StaticResource PhoneTextNormalStyle}" Height="24"/>然后添加一个全局值
private string a = "Test";和init函数中的
this.lbStatus = a;最后我添加了一个按钮并改变了a的值,TextBlock没有改变!为什么?Thx
发布于 2012-03-29 13:30:26
在.NET中,字符串是一种不可变类型。当您键入时:
this.lbStatus = a;将lbStatus设置为对a变量当前指向的字符串的引用。稍后,当您更改a时:
a = "Foo";您不会更改this.lbStatus,因为您将把a变量赋给一个全新的string实例。
发布于 2012-03-29 14:50:33
这可能会帮助你更好地理解
public class Base : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected void NotifyPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
//ViewModel
public class ViewModel : Base
private string _value;
public string value {
get
{
return _value;
}
set
{
_value = value;
this.NotifyPropertyChanged("value");
}
}
//View
<Textbox Height="60" Width="60" Foreground="Wheat"
Text="{Binding value,Mode=TwoWay}" >https://stackoverflow.com/questions/9919564
复制相似问题