Search code examples
c#wpfdata-bindingicommand

wpf command parameter from other object


I'm wondering how to mark up the XAML for the following. I have a view model with an object based on ICommand.

I have a form with a textbox and a button. The button is hooked to the ICommand object via Command="{Binding MyButtonInViewModel}".

What I want to do is set the button's CommandParameter equal to whatever is in the text of the textbox such as to invoke a "Search", but obviously don't know how to hook across controls in the view.


Solution

  • The following XAML should work to pass the Text from the TextBox as Parameter to your command.

    <TextBlock x:Name="searchBox" />
    
    <Button Command="{Binding MyButtonInViewModel}" 
            CommandParameter="{Binding Text, ElementName=searchBox}"/>