假设我有一个MVVM应用程序,我希望用户填写一个文本框,并且在他填写时,检查他是否已经输入了客户的姓氏。
以下是如何让我的ViewModel知道用户何时更改了ComboBox中的项目:
<ComboBox
ItemsSource="{Binding Customers}"
ItemTemplate="{StaticResource CustomerComboBoxTemplate}"
Margin="20"
HorizontalAlignment="Left"
SelectedItem="{Binding SelectedCustomer, Mode=TwoWay}"/>
以下是我如何让ViewModel在用户移动滑块时知道:
<Slider Minimum="0"
Margin="10"
Width="400"
IsSnapToTickEnabled="True"
Maximum="{Binding HighestCustomerIndex, Mode=TwoWay}"
Value="{Binding SelectedCustomerIndex, Mode=TwoWay}"/>
以下是我如何让我的ViewModel知道当用户更改了TextBox中的文本并将焦点移开时:
<TextBox
Width="200"
Text="{Binding TypedCustomerName}"/>
但是我该如何让我的ViewModel知道用户在键入时已经更改了TextBox中的文本,例如:
伪代码(会导致错误,因为TextChanged是一个事件):
<TextBox
Width="200"
TextChanged="{Binding CurrentTextInTextBox}"/>