2009-06-19 8 views
8

J'utilise le modèle MVVM et j'ai une zone de texte dans la fenêtre parente et je veux envoyer du texte à la fenêtre contextuelle qui apparaîtra sur Textchanged.WPF CommandParameter dans la zone de texte

J'ai essayé d'utiliser commandparameter mais cela ne fonctionne pas pour moi.

S'il vous plaît aider ..

Merci Sharath

Répondre

1

Qu'avez-vous essayé? Ce code fonctionne pour moi:

<Window x:Class="WpfApplication1.Window1" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="Window1" Height="300" Width="300"> 
    <Window.CommandBindings> 
     <CommandBinding Command="Cut" Executed="CommandBinding_Executed" /> 
    </Window.CommandBindings> 
    <StackPanel> 
     <TextBox x:Name="textBox1" /> 
     <Button Command="Cut" 
       CommandParameter="{Binding Text,ElementName=textBox1}" 
       Content="Cut" /> 
    </StackPanel> 
</Window> 

Avec ce gestionnaire d'événements:

private void CommandBinding_Executed(object sender, ExecutedRoutedEventArgs e) 
{ 
    MessageBox.Show(e.Parameter.ToString()); 
} 
+0

Je veux utiliser TextChanged ou lorsque textbox utilisateur clique sur entrée du champ de saisie clavier. Je ne veux pas appuyer sur le bouton. –

+0

Si vous utilisez une commande, vous devez utiliser une ICommandSource comme un bouton. Les commandes ne sont pas identiques aux gestionnaires d'événements. –

22

Si je veux la commande à exécuter si l'utilisateur appuie entrer, j'aime utiliser. Notez l'utilisation intelligente de la IsDefault :-) Reliure

<TextBox x:Name="inputBox"/> 
<Button Command="{Binding CutCommand}" 
     CommandParameter="{Binding Text, ElementName=inputBox}" 
     Content="Cut" 
     IsDefault="{Binding IsFocused, ElementName=inputBox}" /> 

Si vous ne voulez pas que le bouton soit visible, vous pouvez définir sa visibilité à effondra bien sûr. Je pense que ça va encore exécuter la commande si vous appuyez sur Entrée.

+0

Merci beaucoup .. Si le bouton est réduit, il ne s'exécute pas. Je fixe la largeur à 0 :-) –

+0

Puis-je faire la même chose pour la liste. Je veux dire quand je Double-cliquez sur les éléments de la liste. Le bouton cliquer même devrait être surélevé. –

+0

Je ne sais pas. J'ai invoqué la commande manuellement dans le code-behind pour cela. Je suis assez nouveau pour xaml, alors qui sait. – Botz3000

2

Ce code fonctionne pour moi

<UserControl x:Class="Test" 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
      mc:Ignorable="d" 
      Height="Auto" Width="Auto"> 
    <UserControl.InputBindings> 
    <KeyBinding Key="Enter" Command="{Binding ScanCommand}" CommandParameter="{Binding Text, ElementName=tbBarcode}"/> 
    </UserControl.InputBindings> 
    <Grid Name="LayoutRoot"> 
    <TextBox x:Name="tbBarcode" Height="23"/> 
    </Grid> 
</UserControl> 
Questions connexes