wpf command parameter from other object

DRapp picture DRapp · Mar 7, 2013 · Viewed 16.4k times · Source

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.

Answer

Jehof picture Jehof · Mar 7, 2013

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}"/>