KeyDown命令不想触发。

时间:2015-09-23 13:40:49

标签: c# wpf mvvm command

所以,我有两个AutocompleteBoxes - 我想在用户按F3或F5时触发一些东西。

Xaml(我正在展示我故意尝试的两种方式):

<my:AutoCompleteTextBox Name="cbStreet"
                                                DataContext="{Binding}"
                                                Provider="{Binding}"
                                                Text="{Binding Path=street,Mode=TwoWay}"
                                                Style="{StaticResource ACStyle}">
                            <my:AutoCompleteTextBox.LoadingContent>
                                <TextBlock Text="Loading..."
                                           Margin="5"
                                           FontSize="14" />
                            </my:AutoCompleteTextBox.LoadingContent>
                            <my:AutoCompleteTextBox.InputBindings>
                                <KeyBinding Key="F3" Command="{Binding Path=StreetF3Command}"/>
                            </my:AutoCompleteTextBox.InputBindings>
                            <i:Interaction.Triggers>
                                <i:EventTrigger EventName="LostFocus">
                                    <i:InvokeCommandAction Command="{Binding Path=StreetLostFocusCommand}" />
                                </i:EventTrigger>
                                <i:EventTrigger EventName="OnKeyDown" >
                                    <Command:EventToCommand  Command="{Binding Path=StreetF5Command}" 
                                     PassEventArgsToCommand="False"  
                                     CommandParameter="{Binding Path=city}"/>
                                </i:EventTrigger>
                            </i:Interaction.Triggers>
                        </my:AutoCompleteTextBox>

带有2个命令的ViewModel。

(...) 
 public ICommand StreetF3Command { get; set; }
 public ICommand StreetF5Command { get; set; }
(...) 
StreetF3Command = new CustomCommand(StreetF3, CanStreetF3);
StreetF5Command = new CustomCommand(StreetF5, CanStreetF5);
(...) 
private bool CanStreetF5(object obj)
        {
            return true;
        }

        private void StreetF5(object obj)
        {
            Messenger.Default.Send<int>(3);
        }

        private bool CanStreetF3(object obj)
        {
            return true;
        }

        private void StreetF3(object obj)
        {
            Messenger.Default.Send<int>(2);
        }
(...) 

事情是 - 当我击中F3或F5时没有任何反应。我尝试调试它,但应用程序甚至没有点击 StreetF5 有趣的是,LostFocus事件有效,而在其他AutoCompleteTextBox非常相似的事件上工作得很好。

的Xaml:

<my:AutoCompleteTextBox Name="cbCity"
                                                Provider="{Binding Path=suggestionProviderCity}"
                                                Style="{StaticResource  ACStyle}"
                                                Text="{Binding Path=city,Mode=TwoWay}"
                                                >
                            <my:AutoCompleteTextBox.LoadingContent>
                                <TextBlock Text="Loading..."
                                           Margin="5"
                                           FontSize="14" />
                            </my:AutoCompleteTextBox.LoadingContent>
                            <my:AutoCompleteTextBox.InputBindings>
                                <KeyBinding Key="F5" Command="{Binding Path=CityF5Command}"/>
                            </my:AutoCompleteTextBox.InputBindings>
                            <i:Interaction.Triggers>
                                <i:EventTrigger>
                                    <i:InvokeCommandAction Command="{Binding Path=CityLostFocusCommand}"/>
                                </i:EventTrigger>
                            </i:Interaction.Triggers>
                        </my:AutoCompleteTextBox>

和ViewModel:

  public ICommand CityF5Command { get; set; }
    CityF5Command = new CustomCommand(CityF5, CanCityF5);
    private void CityF5(object obj)
            {
                Messenger.Default.Send<int>(1);
            }

我没有想法。

0 个答案:

没有答案