Silverlight 如何从ViewModel进行绑定
我试图捕获文本框上按下的Enter键,以便启动服务器更新。它不起作用,所以我把问题归结为最简单的元素 在本例中,绑定似乎不是每次击键都发生,而是在以后某个时间发生。我需要在按下回车键时完成装订。从VM中考虑下面的XAML和函数.< /P> 这是文本框的XAMLSilverlight 如何从ViewModel进行绑定,silverlight,data-binding,mvvm,mvvm-light,Silverlight,Data Binding,Mvvm,Mvvm Light,我试图捕获文本框上按下的Enter键,以便启动服务器更新。它不起作用,所以我把问题归结为最简单的元素 在本例中,绑定似乎不是每次击键都发生,而是在以后某个时间发生。我需要在按下回车键时完成装订。从VM中考虑下面的XAML和函数.< /P> 这是文本框的XAML <TextBox Text="{Binding TextValue, Mode=TwoWay}" Height="23" Width="300"> <i:Interaction.Triggers>
<TextBox Text="{Binding TextValue, Mode=TwoWay}" Height="23" Width="300">
<i:Interaction.Triggers>
<i:EventTrigger EventName="KeyDown">
<cmd:EventToCommand Command="{Binding KeyDownCommand}"
PassEventArgsToCommand="True" />
</i:EventTrigger>
</i:Interaction.Triggers>
</TextBox>
keydown命令按预期启动,但该值尚未在TextValue属性中。如果我第二次按enter键,那么该值是否在属性中?这是keydown命令。ViewModel的构造函数正确设置keydown命令
public RelayCommand<RoutedEventArgs> KeyDownCommand { get; private set; }
private void KeyDownAction(RoutedEventArgs eventArg)
{
var source = eventArg.OriginalSource as FrameworkElement;
var e = eventArg as KeyEventArgs;
if (source != null && e != null && e.Key== Key.Enter)
{
e.Handled = true;
MessageBox.Show(TextValue);
}
}
public RelayCommand keydown命令{get;private set;}
私有void KeyDownAction(routedEventTargets事件arg)
{
var source=eventArg.OriginalSource作为框架元素;
var e=eventArg作为KeyEventArgs;
if(source!=null&&e!=null&&e.Key==Key.Enter)
{
e、 已处理=正确;
MessageBox.Show(TextValue);
}
}
似乎我需要的是一种在按下Enter键时将TextBox的文本“发布”回VM的TextValue属性的方法。或者我还遗漏了什么。我刚发帖,就想到了答案 这是正确的按键动作
private void KeyDownAction(RoutedEventArgs eventArg)
{
var source = eventArg.OriginalSource as FrameworkElement;
source.GetBindingExpression(TextBox.TextProperty).UpdateSource();
var e = eventArg as KeyEventArgs;
if (source != null && e != null && e.Key== Key.Enter)
{
e.Handled = true;
MessageBox.Show(TextValue);
}
}
现在,当我输入这个时,我意识到我正在“打破”这个模式,因为现在我的ViewModel知道更多关于它应该看到的视图的信息。我一发布问题,就想到了答案 这是正确的按键动作
private void KeyDownAction(RoutedEventArgs eventArg)
{
var source = eventArg.OriginalSource as FrameworkElement;
source.GetBindingExpression(TextBox.TextProperty).UpdateSource();
var e = eventArg as KeyEventArgs;
if (source != null && e != null && e.Key== Key.Enter)
{
e.Handled = true;
MessageBox.Show(TextValue);
}
}
现在,当我键入此命令时,我意识到我正在“破坏”模式,因为现在我的ViewModel知道更多关于它应该显示的视图的信息。尝试在绑定时将
UpdateSourceTrigger
设置为PropertyChanged
,如下所示:
<TextBox Text="{Binding TextValue, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Height="23" Width="300">
<TextBox Text="{Binding TextValue, Mode=TwoWay}" Height="23" Width="300">
<i:Interaction.Behaviors>
<b:TextChangedUpdateSourceBehavior />
</i:Interaction.Behaviors>
</TextBox>
像这样使用它:
<TextBox Text="{Binding TextValue, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Height="23" Width="300">
<TextBox Text="{Binding TextValue, Mode=TwoWay}" Height="23" Width="300">
<i:Interaction.Behaviors>
<b:TextChangedUpdateSourceBehavior />
</i:Interaction.Behaviors>
</TextBox>
尝试在绑定时将
UpdateSourceTrigger
设置为PropertyChanged
,如下所示:
<TextBox Text="{Binding TextValue, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Height="23" Width="300">
<TextBox Text="{Binding TextValue, Mode=TwoWay}" Height="23" Width="300">
<i:Interaction.Behaviors>
<b:TextChangedUpdateSourceBehavior />
</i:Interaction.Behaviors>
</TextBox>
像这样使用它:
<TextBox Text="{Binding TextValue, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Height="23" Width="300">
<TextBox Text="{Binding TextValue, Mode=TwoWay}" Height="23" Width="300">
<i:Interaction.Behaviors>
<b:TextChangedUpdateSourceBehavior />
</i:Interaction.Behaviors>
</TextBox>
事实证明,UpdateSuceTrigger=PropertyChanged不是Silverlight的选项。唯一的两个选项是Default和Explicit。@拉尔夫-对不起,没有注意到这个问题是针对Silverlight的。请参阅我对Silverlight解决方案的更新答案。事实证明,UpdateSoceTrigger=PropertyChanged不是Silverlight的选项。唯一的两个选项是Default和Explicit。@拉尔夫-对不起,没有注意到这个问题是针对Silverlight的。请参阅Silverlight解决方案的更新答案。