Pour une raison quelconque, les KeyBindings pour mon UserControl ne fonctionnent pas dès que mon application WPF se charge. Ils fonctionnent après que j'appuie sur un bouton sur le formulaire, mais pas quand je mets le focus sur le formulaire en cliquant ou alt tabulation ou en mouvement ou quelque chose comme ça. Et quand ils fonctionnent, mes touches d'entrée impriment un nombre aléatoire. (parfois 5, parfois 7 etc ...).WPF MVVM KeyBinding n'est pas reconnu immédiatement et ne fonctionne pas toujours
<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>
J'ai vraiment pas rencontré quelqu'un d'autre avec ce problème, donc je ne suis pas vraiment sûr de quoi donner plus que cela. Faites-moi savoir s'il y a d'autres informations requises?
Je vais essayer d'utiliser CommandReference pour lier les entrées comme vous le suggérez. J'ai actuellement Visual Studio 2010 donc je n'ai pas essayé mole. (Connaissez-vous peut-être un autre moyen de vérifier cela?) Mais je suis un peu confus au sujet de certaines informations présentées dans l'article. Selon l'article, je devrais obtenir une exception, mais pour le moment, ma demande est en cours d'exécution à court de problèmes que je suis en train de traiter en ce moment. – Terrance
Votre réponse a aidé le comportement du bouton, mais je devais utiliser FocusManager.FocusedElement = "{Binding ElementName = input}" pour définir le focus de la zone de texte. Mais la référence de commande n'a fait aucun changement à l'application bien que je vais certainement le garder à l'esprit pour attacher des propriétés de non-dépendance quand je suis dans une liaison à l'avenir. Pas de jeu de mots stupide. lol – Terrance
Un exemple de ClickMode: J'ai mentionné eariler – Terrance