1 回答
TA贡献2011条经验 获得超2个赞
请检查UpdateSourceTrigger文档。
默认的 UpdateSourceTrigger 值为Default。并使用来自使用绑定的依赖项属性的默认行为。在 Windows 运行时中,这与带有 的值的计算结果相同PropertyChanged。如果您使用Text="{x:Bind ViewModel.Title, Mode=TwoWay}",标题将在文本更改时更改。我们不需要修改TextChanged偶数处理程序中的视图模式。
前提是我们需要INotifyPropertyChanged像下面这样实现。
public class HostViewModel : INotifyPropertyChanged
{
private string nextButtonText;
public event PropertyChangedEventHandler PropertyChanged = delegate { };
public HostViewModel()
{
this.NextButtonText = "Next";
}
public string NextButtonText
{
get { return this.nextButtonText; }
set
{
this.nextButtonText = value;
this.OnPropertyChanged();
}
}
public void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
// Raise the PropertyChanged event, passing the name of the property whose value has changed.
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
有关更多详细信息,请参阅深度文档中的数据绑定。
更新
<TextBox Text="{x:Bind Title, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />根本不编译,正如我所说,UpdateSourceTrigger使用编译绑定时该属性根本不可用。
对于我的测试,它运行良好。Compiled Binding 与Classic Binding{x:Bind}语法相对的语法一起使用。{Binding}它仍然使用通知接口(如INotifyPropertyChanged)来监视更改,但{x:Bind}默认情况下是 OneTime ,{Binding}而 OneWay 是。所以你需要声明 bind Mode OneWay或TwoWayfor {x:Bind}。
Xaml
<StackPanel Orientation="Vertical">
<TextBox Text="{x:Bind Title, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
<TextBlock Text="{x:Bind Title, Mode=OneWay}" /> <!--declare bind mode-->
</StackPanel>
代码隐藏
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
private string _title;
public string Title
{
get
{
return _title;
}
set
{
_title = value;
OnPropertyChanged();
}
}
- 1 回答
- 0 关注
- 139 浏览
添加回答
举报
