【问题标题】:How do I pass the textbox text in a command as a parameter WPF如何将命令中的文本框文本作为参数 WPF 传递
【发布时间】:2018-11-13 00:45:43
【问题描述】:

所以我的 MainWindow.xaml 上有一个文本框。

<Window x:Class="HelloICommand.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        ...
    <Grid>
        <TextBox HorizontalAlignment="Left" Height="23" Margin="337,195,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120">
            <TextBox.InputBindings>
                <KeyBinding Command="{Binding }" Key="Enter"></KeyBinding>
            </TextBox.InputBindings>
        </TextBox>
    </Grid>
</Window>

如您所见,我想将我的回车键绑定到我可以点击回车的位置,它会显示一个消息框,其中包含文本框中的文本。

在我的 MainWindow.cs 中,我像这样设置数据上下文。

public MainWindow()
        {
            InitializeComponent();
            DataContext = new ServerViewModel();
        }

然后我有了实际的 ServerViewModel,里面还有其他所有东西 这就是我遇到问题的地方,如何将文本从 TextBox 传递到该方法,以便每次单击 Enter 时都能看到消息。

class ServerViewModel
{
    private TextBoxCommand textCommand { get; private set; }
    public ServerViewModel()
    {
        textCommand = new TextBoxCommand(SendMessage);
    }

    //How do I pass the text from the textbox as a parameter here?
    public void SendMessage()
    {
        MessageBox.Show("");
    }
}

ICommand 接口

class TextBoxCommand : ICommand
    {
        public Action _sendMethod;

        public TextBoxCommand(Action SendMethod)
        {
            _sendMethod = SendMethod;
        }
        public bool CanExecute(object parameter)
        {
            return true;
        }

        public void Execute(object parameter)
        {

        }

        public event EventHandler CanExecuteChanged;
    }

【问题讨论】:

    标签: c# .net wpf mvvm command


    【解决方案1】:

    视图只能绑定到公共属性的命令。这意味着第一步是将您的命令定义为公共(只读)属性:

    public TextBoxCommand TextCommand { get; }
    

    ICommand 接口允许将对象作为参数传递给其执行函数。如果您的命令实现,TextBoxCommand 允许传递此参数,只需将参数添加到您的方法,将其转换为字符串并显示您的消息:

    private void SendMessage(object parameter)
    {
        MessageBox.Show((string)parameter);
    }
    

    如果您的TextBoxCommand 不允许传递参数,那么像How to implement a reusable ICommand 所示的简单实现就可以解决问题。只需将您的 TextBoxCommand 替换为教程中的 DelegateCommand

    要从您的视图中正确执行命令,您现在需要将命令绑定到TextCommand 属性。第二步,将文本框的文本设置为命令参数。使用此名称,您可以绑定到 Text 属性并将它们作为参数传递给您的命令。因此,您需要命名您的文本框。这是最小的例子:

    <TextBox x:Name="yourTextBox>
        <TextBox.InputBindings>
            <KeyBinding Key="Enter"
                        Command="{Binding TextCommand}"
                        CommandParameter="{Binding Text, ElementName=yourTextBox}"/>
        </TextBox.InputBindings>
    </TextBox>
    

    【讨论】:

    • 但是我的构造函数呢,你可以在我的示例中看到我正在为我的属性分配一个值textCommand = new TextBoxCommand(SendMessage); 我如何将它传递给正确的参数,因为SendMessage() 不将对象作为参数所以这样做textCommand = new TextBoxCommand(SendMessage());会返回一个错误,因为没有参数
    • 是的,这可能是您的TextBoxCommand 的问题。我会为我的答案添加一个解决方案。
    • 你在视图模型中使用 MessageBox.Show 吗?
    • 通常你不会。一种可能的解决方案是使用MessageBox using MVVM Patterns 中描述的注入服务。由于这不是问题的范围,因此我不会在回答中对此进行处理。
    猜你喜欢
    • 2013-10-01
    • 1970-01-01
    • 2017-12-17
    • 2010-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-18
    相关资源
    最近更新 更多