Mar*_*ari 3 c# xaml win-universal-app
我正在尝试将xaml中TextBlock的'Text'属性绑定到全局字符串,但是当我更改字符串时,TextBlock的内容不会改变.我错过了什么?
我的xaml:
<StackPanel>
<Button Content="Change!" Click="Button_Click" />
<TextBlock Text="{x:Bind text}" />
</StackPanel>
Run Code Online (Sandbox Code Playgroud)
我的C#:
string text;
public MainPage()
{
this.InitializeComponent();
text = "This is the original text.";
}
private void Button_Click(object sender, RoutedEventArgs e)
{
text = "This is the changed text!";
}
Run Code Online (Sandbox Code Playgroud)
Mic*_*ger 11
默认的结合模式x:Bind是OneTime甚则OneWay认为是事实上的默认了Binding.而且text是private.要有一个工作绑定,你需要有一个public property.
<TextBlock Text="{x:Bind Text , Mode=OneWay}" />
Run Code Online (Sandbox Code Playgroud)
在代码隐藏中
private string _text;
public string Text
{
get { return _text; }
set
{
_text = value;
NotifyPropertyChanged("Text");
}
Run Code Online (Sandbox Code Playgroud)
另外,在Text的setter中引发PropertyChanged很重要.