0

在没有代码隐藏的情况下连接 keydown 事件有一点问题!所以,我们有组合框

<ComboBox Height="20" Width="auto"
                  ItemsSource="{Binding AlignComboItems}"
                  SelectedValue="{Binding SelectedComboItem, Mode=TwoWay}"
                  SelectedValuePath="Key" DisplayMemberPath="Value"

                  SelectedItem="{Binding SelectedItem}"
                  x:Name="cmbBoxAlign">
</ComboBox>

和一些文本框。

<TextBox Text={Binding SomeSource}></TextBox>

如何捕获 TextBox 上的 keydown 事件以选择(例如)ComboBox 中的最后一个元素?我不能使用 TextBox DataSource 属性更改,因为需要挂钩用户输入。

4

2 回答 2

4

如果您不介意安装 Expression Blend SDK,您应该可以在您的文本框中执行此操作

<i:Interaction.Triggers>
    <i:EventTrigger EventName="KeyUp">
        <i:InvokeCommandAction Command="{Binding Path=TheCommand}" />
    </i:EventTrigger>
</i:Interaction.Triggers>

System.Windows.Interactivity在您的 xaml 中添加对以下命名空间的引用后

xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"

链接到 Expression SDK for 4.0

http://www.microsoft.com/en-us/download/details.aspx?id=10801

于 2013-02-13T06:47:39.440 回答
0

如果您希望每次在文本框中按下键时在视图模型中触发代码,您需要稍微更改绑定:

<TextBox Text="{Binding SomeSource, UpdateSourceTrigger=PropertyChanged}"

然后在视图模型中将调用 setter:

private string _someSource;
public string SomeSource{
  get { return _someSource; }
  set { 
    //this will fire on key down
    _someSource= value;
    //based off the value you can set SelectedComboItem accordingly
    OnPropertyChanged( "SomeSource" );
  }
}

此外,请确保您在视图模型上设置了 INotifyPropertyChanged。

于 2013-02-12T22:30:24.373 回答