2009-06-19 19 views
8

私はMVVMパターンを使用していますが、親ウィンドウにテキストボックスがあり、ポップアップウィンドウにテキストを送信してTextchangedに表示します。WPF CommandParameter in Textbox

私はcommandparameterを使用しようとしましたが、それは私のために働いていません。

助けてください..

おかげ シャラス

答えて

1

あなたは何を試してみましたか?このコードは、私の作品:

このイベントハンドラでは
<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> 

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

。 ボタンしたくありません。 –

+0

コマンドを使用している場合、ボタンのようなICommandSourceを使用する必要があります。コマンドはイベントハンドラと同じではありません。 –

22

ユーザーを押すと入力した場合、私は、コマンドを実行したい場合は、私はこれを使用したいです。あなたはボタンが表示されるようにしたくない場合は、当然の崩壊にその可視性を設定することができ、

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

:-)バインディングのIsDefaultの巧妙な使用に注意してください。私はあなたがenterを押した場合でもコマンドを実行すると思います。

+0

ありがとうございます。ボタンが倒れても実行されません。 幅を0に設定しました:-) –

+0

リストボックスでも同じことができますか?私はリストボックスの項目をダブルクリックすると意味します。ボタンのクリックも上げなければなりません。 –

+0

私は分かりません。私はそのためのコードビハインドでコマンドを手動で呼び出しました。私はxamlにはかなり新しいです、だから誰が知っている。 – Botz3000

2

このコードは動作しますため、私はテキストボックスtextchangedまたはユーザーがクリックキーボードからテキストボックスに入力するときに使用したい私

<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>