所以,假设我有一个MVVM应用程序,我希望用户填写一个TextBox,当他填写它时,我想查看他是否输入了客户的姓氏.
以下是我如何让我的ViewModel知道用户何时更改了ComboBox中的项目:
<ComboBox
ItemsSource="{Binding Customers}"
ItemTemplate="{StaticResource CustomerComboBoxTemplate}"
Margin="20"
HorizontalAlignment="Left"
SelectedItem="{Binding SelectedCustomer, Mode=TwoWay}"/>
Run Code Online (Sandbox Code Playgroud)
以下是我如何让我的ViewModel知道用户何时移动了Slider:
<Slider Minimum="0"
Margin="10"
Width="400"
IsSnapToTickEnabled="True"
Maximum="{Binding HighestCustomerIndex, Mode=TwoWay}"
Value="{Binding SelectedCustomerIndex, Mode=TwoWay}"/>
Run Code Online (Sandbox Code Playgroud)
以下是我如何让我的ViewModel知道用户何时更改了TextBox中的文本并将焦点从TextBox 移开:
<TextBox
Width="200"
Text="{Binding TypedCustomerName}"/>
Run Code Online (Sandbox Code Playgroud)
但是,如何让我的ViewModel知道用户何时更改TextBox中的文本,例如:
PSEUDO-CODE(因为TextChanged是一个事件导致错误):
<TextBox
Width="200"
TextChanged="{Binding CurrentTextInTextBox}"/>
Run Code Online (Sandbox Code Playgroud)
rmo*_*ore 11
如果您愿意,可以在TextBox失去焦点时更新ViewModel,而不是在键入时将其设置为更新.该UpdateSourceTrigger在文本框的文本绑定属性默认替代的PropertyChanged像大多数其他控件设置为引发LostFocus,但是你可以在绑定明确设置它.通过这样做,VM或M中的TypedCustomerName属性将在UI中更改时更新.
<TextBox
Width="200"
Text="{Binding TypedCustomerName, UpdateSourceTrigger=PropertyChanged}"/>
Run Code Online (Sandbox Code Playgroud)
如果这不是您要查找的内容,您还可以使用AttachedCommandBehaviors将TextChanged路由事件绑定到View模型中存在的ICommand.
| 归档时间: |
|
| 查看次数: |
2604 次 |
| 最近记录: |