2010-04-22 53 views
3

無論出於何種原因,當我的WPF應用程序加載時,我的UserControl的KeyBindings無法工作。當我按下表格上的按鈕後,它們會工作,但當我通過單擊或切換標籤或移動或任何類似的東西來將焦點設置到表單上時,它們就會工作。當他們工作時,我的輸入鍵打印一個隨機數字。 (有時5,有時7等)。WPF MVVM KeyBinding不會被馬上識別,並且不總是正常工作

<UserControl x:Class="WpfCalculator.View.CalculatorView" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Height="300" Width="300" 
    > 
<UserControl.InputBindings> 
    <KeyBinding Key="DELETE" Command="{Binding Path=IBackspaceOnInput}" /> 
    <KeyBinding Key="BACKSPACE" Command="{Binding Path=IBackspaceOnInput}" /> 

    <KeyBinding Key="NUMPAD0" Command="{Binding Path=IAppendToUserInput}" CommandParameter="0" /> 
    <KeyBinding Key="NUMPAD1" Command="{Binding Path=IAppendToUserInput}" CommandParameter="1" /> 
    <KeyBinding Key="NUMPAD2" Command="{Binding Path=IAppendToUserInput}" CommandParameter="2" /> 
    <KeyBinding Key="NUMPAD3" Command="{Binding Path=IAppendToUserInput}" CommandParameter="3" /> 
    <KeyBinding Key="NUMPAD4" Command="{Binding Path=IAppendToUserInput}" CommandParameter="4" /> 
    <KeyBinding Key="NUMPAD5" Command="{Binding Path=IAppendToUserInput}" CommandParameter="5" /> 
    <KeyBinding Key="NUMPAD6" Command="{Binding Path=IAppendToUserInput}" CommandParameter="6" /> 
    <KeyBinding Key="NUMPAD7" Command="{Binding Path=IAppendToUserInput}" CommandParameter="7" /> 
    <KeyBinding Key="NUMPAD8" Command="{Binding Path=IAppendToUserInput}" CommandParameter="8" /> 
    <KeyBinding Key="NUMPAD9" Command="{Binding Path=IAppendToUserInput}" CommandParameter="9" /> 

    <KeyBinding Key="D0" Command="{Binding Path=IAppendToUserInput}" CommandParameter="0" /> 
    <KeyBinding Key="D1" Command="{Binding Path=IAppendToUserInput}" CommandParameter="1" /> 
    <KeyBinding Key="D2" Command="{Binding Path=IAppendToUserInput}" CommandParameter="2" /> 
    <KeyBinding Key="D3" Command="{Binding Path=IAppendToUserInput}" CommandParameter="3" /> 
    <KeyBinding Key="D4" Command="{Binding Path=IAppendToUserInput}" CommandParameter="4" /> 
    <KeyBinding Key="D5" Command="{Binding Path=IAppendToUserInput}" CommandParameter="5" /> 
    <KeyBinding Key="D6" Command="{Binding Path=IAppendToUserInput}" CommandParameter="6" /> 
    <KeyBinding Key="D7" Command="{Binding Path=IAppendToUserInput}" CommandParameter="7" /> 
    <KeyBinding Key="D8" Command="{Binding Path=IAppendToUserInput}" CommandParameter="8" /> 
    <KeyBinding Key="D9" Command="{Binding Path=IAppendToUserInput}" CommandParameter="9" /> 

    <KeyBinding Key="ADD" Command="{Binding Path=IAppendToUserInput}" CommandParameter="+" /> 
    <KeyBinding Key="SUBTRACT" Command="{Binding Path=IAppendToUserInput}" CommandParameter="-" /> 
    <KeyBinding Key="MULTIPLY" Command="{Binding Path=IAppendToUserInput}" CommandParameter="*" /> 
    <KeyBinding Key="DIVIDE" Command="{Binding Path=IAppendToUserInput}" CommandParameter="/" /> 

    <KeyBinding Key="Return" Command="{Binding Path=ICalculateExpression}" CommandParameter="" /> 
    <KeyBinding Key="Enter" Command="{Binding Path=ICalculateExpression}" CommandParameter="" /> 
    <KeyBinding Key="Escape" Command="{Binding Path=IClearInput}" CommandParameter="" /> 

    <KeyBinding Gesture="CTRL+M" Command="{Binding Path=IRememberExpression}" CommandParameter="" /> 
    <KeyBinding Gesture="CTRL+R" Command="{Binding Path=IRecallExpression}" CommandParameter="" /> 
    <KeyBinding Gesture="CTRL+X" Command="{Binding Path=IForgetExpression}" CommandParameter="" /> 

    <KeyBinding Key="Left" Command="{Binding Path=IMMoveCursor}" CommandParameter="1" /> 
    <KeyBinding Key="Right" Command="{Binding Path=IMMoveCursor}" CommandParameter="-1" /> 

</UserControl.InputBindings> 

<Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Height="305" Width="489"> 
    <TextBox Name="input" HorizontalAlignment="Right" IsReadOnly="True" Margin="0,12,200,271" Text="{Binding Path=UserInput}" Width="275" /> 
    <Button Content="CE" Margin="143,0,323,147" Command="{Binding Path=IBackspaceOnInput}" CommandParameter="" Height="23" VerticalAlignment="Bottom" /> 
    <Button Content="1" Height="23" HorizontalAlignment="Right" Margin="0,106,294,176" Width="23" Command="{Binding Path=IAppendToUserInput}" CommandParameter="1" /> 
    <Button Content="2" Height="23" HorizontalAlignment="Right" Margin="0,106,265,176" Width="23" Command="{Binding Path=IAppendToUserInput}" CommandParameter="2" /> 
    <Button Content="3" Height="23" HorizontalAlignment="Right" Margin="0,106,236,176" Width="23" Command="{Binding Path=IAppendToUserInput}" CommandParameter="3" /> 
    <Button Content="4" Height="23" HorizontalAlignment="Right" Margin="0,77,294,205" Width="23" Command="{Binding Path=IAppendToUserInput}" CommandParameter="4" /> 
    <Button Content="5" Height="23" HorizontalAlignment="Right" Margin="0,77,236,205" Width="23" Command="{Binding Path=IAppendToUserInput}" CommandParameter="5" /> 
    <Button Content="6" Height="23" HorizontalAlignment="Right" Margin="0,77,265,205" Width="23" Command="{Binding Path=IAppendToUserInput}" CommandParameter="6" /> 
    <Button Content="7" Height="23" HorizontalAlignment="Right" Margin="0,48,294,234" Width="23" Command="{Binding Path=IAppendToUserInput}" CommandParameter="7" /> 
    <Button Content="8" Height="23" HorizontalAlignment="Right" Margin="0,48,265,234" Width="23" Command="{Binding Path=IAppendToUserInput}" CommandParameter="8" /> 
    <Button Content="9" Height="23" HorizontalAlignment="Right" Margin="0,48,236,234" Width="23" Command="{Binding Path=IAppendToUserInput}" CommandParameter="9" /> 
    <Button Content="0" Height="23" HorizontalAlignment="Left" Margin="201,135,0,147" Width="23" Command="{Binding Path=IAppendToUserInput}" CommandParameter="0" /> 

    <Button Content="+" Height="23" HorizontalAlignment="Right" Margin="0,48,201,234" Width="23" Command="{Binding Path=IAppendToUserInput}" CommandParameter="+" /> 
    <Button Content="-" Height="23" HorizontalAlignment="Right" Margin="0,77,201,205" Width="23" Command="{Binding Path=IAppendToUserInput}" CommandParameter="-" /> 
    <Button Content="*" Height="23" HorizontalAlignment="Right" Margin="0,106,201,176" Width="23" Command="{Binding Path=IAppendToUserInput}" CommandParameter="*" /> 
    <Button Content="/" Height="23" HorizontalAlignment="Right" Margin="0,135,201,147" Width="23" Command="{Binding Path=IAppendToUserInput}" CommandParameter="/" /> 
    <Button Content="." Height="23" HorizontalAlignment="Right" Margin="0,135,236,147" Width="23" Command="{Binding Path=IAppendToUserInput}" CommandParameter="9" /> 

    <Button Content="C" HorizontalAlignment="Right" Margin="0,164,323,118" Width="23" Command="{Binding Path=IClearInput}" CommandParameter="" /> 
    <Button Content="MC" Height="23" HorizontalAlignment="Right" Margin="0,0,323,234" VerticalAlignment="Bottom" Width="23" Command="{Binding Path=IForgetExpression}" CommandParameter=""/> 
    <Button Content="M+" Height="23" HorizontalAlignment="Right" Margin="0,0,323,176" VerticalAlignment="Bottom" Width="23" Command="{Binding Path=IRememberExpression}" CommandParameter=""/> 
    <Button Content="MR" Height="23" HorizontalAlignment="Right" Margin="0,0,323,205" VerticalAlignment="Bottom" Width="23" Command="{Binding Path=IRecallExpression}" CommandParameter=""/> 

    <Expander ExpandDirection="Left" Header="History" Height="91" HorizontalAlignment="Right" Margin="0,193,200,0" VerticalAlignment="Top" Width="275"> 
     <ListView ItemsSource="{Binding Path=History}" > 
      <ListView.View> 
       <GridView > 
        <GridViewColumn Header="Start" DisplayMemberBinding="{Binding Started}"/> 
        <GridViewColumn Header="End" DisplayMemberBinding="{Binding Completed}"/> 
        <GridViewColumn Header="Expression" DisplayMemberBinding="{Binding Calculation}"/> 
        <GridViewColumn Header="Solution" DisplayMemberBinding="{Binding Result}"/> 
       </GridView> 
      </ListView.View> 
     </ListView> 
    </Expander> 
    <Button Command="{Binding Path=ICalculateExpression}" CommandParameter="" IsEnabled="{Binding Path=IsEqualsBtnEnabled}" Content="=" Height="23" Margin="172,0,236,118" VerticalAlignment="Bottom" /> 

</Grid></UserControl> 

我真的還沒有碰到任何人有這方面的問題來了,所以我真的不知道該怎麼給比這更多。讓我知道是否有其他需要的信息?

回答

6

您是否驗證過使用Mole加載時控件實際上具有焦點?可能是,無論你的父母控制是否保持焦點,直到手動選擇你的按鈕。至於Enter鍵,聽起來好像他們可能選擇了最後點擊的按鈕,因爲它仍然有焦點而不是發射你的命令。

您可能需要查看您的命令,因爲我不確定聲明是否設置正確。對於KeyBinding s,您的命令應在XAML中引用爲CommandReference,如this article所述。

+0

我將嘗試使用CommandReference來綁定輸入,如你所建議的。我目前有Visual Studio 2010,所以我沒有嘗試過鼴鼠。 (你也許知道另一種檢查方法嗎?)但是我對文章中提供的一些信息有點困惑。根據文章,我應該得到一個例外,但現在,但我的應用程序運行良好,目前我正在處理的問題。 – Terrance 2010-04-22 17:00:39

+0

您的回答確實有助於按鈕點擊行爲,但我只需要使用 FocusManager.FocusedElement =「{Binding ElementName = input}」 設置文本框的焦點。但是Command Reference並沒有對應用做任何改變,但是當我將來遇到綁定時,我肯定會記住附加非依賴屬性。沒有愚蠢的約束雙關語意圖。 lol – Terrance 2010-04-22 17:55:49

+0

ClickMode的一個例子:我提到了收銀臺

1

我遇到了同樣的問題。我跟進了焦點問題。我發現,當我將自己的注意力集中在我的表單中的一個按鈕上時,這些命令開始工作。如果我的焦點項目被刪除而沒有重定向(例如從列表框中刪除最後一項),那麼我的命令都不起作用。

我手動將我的鍵盤焦點重置爲在離開我的焦點的情況下的默認項目,導致我的命令中斷。在我的情況下,這是頁面加載事件和刪除命令(當我的列表框中沒有更多的項目)。

相關問題