I’m working on a WPF app, using .NET 4. I’m also trying to wrap my head about WPF Commands, and not always rely upon events, as I’ve done for upteen years. I’m trying to figure out which command to use. I’ve come across a Search command, that that appears to be a part of the NavigationCommands, which doesn’t make sense to me to use. I’ve also found the Find command, but that’s associated with Control-F, which makes me think its for finding a search in a document, like a Word document, so again, that doesn’t look right to me.
Here’s what I want to accomplish. I will have some textboxes on the windows, for things like first name, last name, SSN and DOB. I want to make it possible for the user to enter any or all of those, and then click on a button. It then performs a search against our database for all records matching the parameters the user gave. It will then put the returned results in a list box on the same window.
What WPF commmand do I use for that?
No WPF Command will do this magically for you.
You will have to write code for this.
Now there are two options…
MVVMpattern then you will have to use DelegateCommands.RoutedComamndswill do.For RoutedCommands you could use a custom routed command OR use existing NavigationCommand.Search OR Find Commands, all you have to remember is to apply
CommandBindings. UsingCommandBindings, you have to provide anOnExecutedhandler in which your logic will be put.Now to extract what user have typed in the TextBoxes, you could bind the textboxes to the command using
CommandParameter…e.g.
Below is a simply code that displays “Hello World ” when user types his / her name in the TetxBox and hits “Hit Me!” button, which has the command configured for it. The command also disables the button if user clears the textbox (i.e. invalid input).
XAML
Code Behind …
Hope this helps…