【发布时间】:2014-02-28 05:30:34
【问题描述】:
我有 2 个“文本框”都绑定到具有“mode =2way”的源字符串属性。当我将文本更改为一个时,另一个会完美更改。但是当我以编程方式更改源字符串时,都不会更新。我无法弄清楚我错过了什么。这是我的代码 sn-ps:
Xaml 代码:
<StackPanel Orientation="Vertical">
<StackPanel.DataContext>
<local:x/>
</StackPanel.DataContext>
<TextBox Text="{Binding Text,Mode=TwoWay}" />
<TextBox Text="{Binding Text, Mode=TwoWay}"/>
</StackPanel>
<Button Content="Reset" Click="Button_Click"/>
按钮点击处理程序:
private void Button_Click(object sender, RoutedEventArgs e)
{
obj = new x() { Text="reset success"};
}
对象类:
class x:INotifyPropertyChanged
{
private string text;
public string Text
{
get { return text; }
set
{
text = value;
OnPropertyChange("Text");
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChange(string propertyName)
{
PropertyChangedEventHandler propertyChangedEvent = PropertyChanged;
if (propertyChangedEvent != null)
{
propertyChangedEvent(this, new PropertyChangedEventArgs(propertyName));
}
}
}
【问题讨论】:
-
我通过在代码中设置stackpanel的datacontext找到了一种解决方法
-
请检查答案或向我们添加更多帮助
标签: c# .net xaml data-binding windows-store-apps