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
或TwoWay
for {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 關注
- 93 瀏覽
添加回答
舉報