【发布时间】:2011-07-24 17:54:08
【问题描述】:
绑定属性更改时绑定更新出现问题。 看看下面的代码。我将在以下示例中解释我的问题。
public class SettingsControl : INotifyPropertyChanged
{
string _value = "test";
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string name)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(name));
}
}
public SettingsControl() { }
public string Value
{
get { return _value; }
set { _value = value; OnPropertyChanged("Value"); }
}
}
<local:SettingsControl x:Key="Settings"></local:SettingsControl>
<TextBox Height="72" Text="{Binding Value, Mode=TwoWay, Source={StaticResource Settings} }"/>
<TextBlock Text="{Binding Value, Mode=OneWay, Source={StaticResource Settings} }" VerticalAlignment="Top" Width="135" />
<Button Height="100" Click="button1_Click" />
和后面的代码:
private void button1_Click(object sender, RoutedEventArgs e)
{
SettingsControl settings = new SettingsControl();
settings.Value = "new value";
}
现在,当我更改 TextBox 中的文本时,一切正常。新文本显示在 TextBlock 中。
但是,如果我将代码中的新文本设置为 settings.Value,则不会发生任何事情。
为了更改代码中的settings.Value 并影响TextBlock 中的TextProperty,我应该做些什么。
编辑:以下针对与我有相同问题的人的解决方案:
SettingsControl settings = (SettingsControl)this.Resources["Settings"];
settings.Value = "new value";
【问题讨论】:
标签: c# silverlight windows-phone-7 binding