Mvvm KeyDown的实现以及TextBox绑定的属性不更新问题的解决

今天写程序的时候遇到了个问题:使用mvvm给TextBox在vm层实现KeyDown键盘事件时,TextBox绑定的vm层属性不更新。

但在TextBox下加个按钮时就更新了。这个问题困扰了我好一会,最后才想起来是更新时机没有给定,所以默认的是LostFocus,故改为PropertyChanged就好了。

 1 <Window x:Class="TestDemo1.MainWindow"
 2         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 3         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 4         xmlns:cmd="http://www.galasoft.ch/mvvmlight"
 5         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
 6         xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
 7         xmlns:ignore="http://www.galasoft.ch/ignore"
 8         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
 9         Title="MVVM Light Application"
10         Width="300"
11         Height="300"
12         DataContext="{Binding Main,
13                               Source={StaticResource Locator}}"
14         mc:Ignorable="d ignore">
15 
16     <Window.Resources>
17         <ResourceDictionary>
18             <ResourceDictionary.MergedDictionaries>
19                 <ResourceDictionary Source="Skins/MainSkin.xaml" />
20             </ResourceDictionary.MergedDictionaries>
21         </ResourceDictionary>
22     </Window.Resources>
23 
24     <Grid x:Name="LayoutRoot">
25         <StackPanel>
26             <TextBlock Margin="0,50,0,0"
27                        HorizontalAlignment="Center"
28                        VerticalAlignment="Center"
29                        FontWeight="Bold"
30                        Foreground="Purple"
31                        Text="{Binding WelcomeTitle}"
32                        TextWrapping="Wrap" />
33             <TextBox Margin="20" Text="{Binding MyField, UpdateSourceTrigger=PropertyChanged}">
34                 <i:Interaction.Triggers>
35                     <i:EventTrigger EventName="KeyDown">
36                         <cmd:EventToCommand Command="{Binding MyTestCommand}" />
37                     </i:EventTrigger>
38                 </i:Interaction.Triggers>
39             </TextBox>
40             <Button Height="20"
41                     Command="{Binding CmdBtn}"
42                     Content="Button" />
43         </StackPanel>
44     </Grid>
45 </Window>

 链接:http://files.cnblogs.com/files/zhangyongheng/TestDemo1.rar

转载于:https://www.cnblogs.com/zhangyongheng/p/6510058.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
假设你有一个名为Student的类,其中包含一个名为Name的属性。现在,你想使用MVVM模式将一个TextBox的Text属性到Student对象的Name属性。可以按照以下步骤操作: 1. 在ViewModel中创建一个名为Student的属性,它是Student类的一个实例。如下所示: ```csharp public class MyViewModel : INotifyPropertyChanged { private Student _student; public Student Student { get { return _student; } set { if (_student != value) { _student = value; OnPropertyChanged("Student"); } } } // ... } ``` 2. 在XAML中创建一个TextBox,并将Text属性到Student对象的Name属性。如下所示: ```xaml <TextBox Text="{Binding Student.Name, Mode=TwoWay}" /> ``` 注意,这里使用了“Student.Name”路径,以便到Student对象的Name属性。还设置了Mode=TwoWay,以便允许双向,即当Student对象的Name属性更改时,TextBox的内容也会更新。 3. 在Window或UserControl的代码中,将ViewModel实例设置为DataContext。如下所示: ```csharp public partial class MyView : Window { public MyView() { InitializeComponent(); DataContext = new MyViewModel { // 初始化Student对象 Student = new Student { Name = "John Doe" } }; } } ``` 现在,当你运行应用程序时,TextBox将显示Student对象的Name属性的值。当你更改TextBox的内容时,Student对象的Name属性也会更新。请注意,ViewModel中的Student属性必须实现INotifyPropertyChanged接口,以便在Student对象更改时通知UI更新

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值