私はMVVMパターンを使用しており、親ウィンドウにテキストボックスがあり、Textchangedに表示されるポップアップウィンドウにテキストを送信したいと考えています。
commandparameter を使用してみましたが、うまくいきません。
助けてください..
ありがとうシャラス
ユーザーがEnterキーを押したときにコマンドを実行したい場合は、これを使用したいと思います。IsDefault Binding の賢い使い方に注意してください :-)
<TextBox x:Name="inputBox"/>
<Button Command="{Binding CutCommand}"
CommandParameter="{Binding Text, ElementName=inputBox}"
Content="Cut"
IsDefault="{Binding IsFocused, ElementName=inputBox}" />
ボタンを表示したくない場合は、もちろんその表示を折りたたむように設定できます。Enterキーを押してもコマンドが実行されると思います。
このコードは私のために働く
<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>
何を試しましたか?このコードは私のために働きます:
<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());
}