文本框中的WPF CommandParameter

10 wpf command textbox mvvm

我正在使用MVVM模式,我在父窗口中有一个文本框,并希望将一些文本发送到弹出窗口,该窗口将显示在Textchanged上.

我尝试使用commandparameter,但它不适合我.

请帮忙..

谢谢沙拉斯

Bot*_*000 28

如果我想要在用户按Enter键时执行该命令,我想使用它.注意聪明地使用IsDefault绑定:-)

<TextBox x:Name="inputBox"/>
<Button Command="{Binding CutCommand}" 
        CommandParameter="{Binding Text, ElementName=inputBox}" 
        Content="Cut" 
        IsDefault="{Binding IsFocused, ElementName=inputBox}" />
Run Code Online (Sandbox Code Playgroud)

如果您不希望该按钮可见,则可以将其可见性设置为折叠.我认为如果你按下回车它仍会执行命令.


Mat*_*ton 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>
Run Code Online (Sandbox Code Playgroud)

使用此事件处理程序:

private void CommandBinding_Executed(object sender, ExecutedRoutedEventArgs e)
{
    MessageBox.Show(e.Parameter.ToString());
}
Run Code Online (Sandbox Code Playgroud)