1

Looking through the standard WPF commands, such as copy/paste, they seem to all work using one button and act on a textbox.

My question: how do I use commands when I have one button, but I need data to be set in two separate controls(a textbox and a combobox). If the user has written text in textbox, but not selected a combobox value, then CanExecute should fail. This applies if combobox has been set, but not the textbox.

In my case specifically, all these controls are wrapped in a tabitem. As well, I have another tab with only a textbox and a button. I want it to have the same functionality as the first tab, except, instead of checking for the combobox value, it should detect that there is no combobox and pass in a default 'null object' value instead.

The Execute method should call a method in my viewmodel and pass in values from the combobox and textbox. As well, each tab should pass in another unique static value; i think i can handle this using commandparameter though.

How do I make this work? Do I make the parent tab that commandtarget and directly reference its children controls in the can/execute methods?

H.B.
  • 166,899
  • 29
  • 327
  • 400
FZdev
  • 418
  • 1
  • 5
  • 10
  • TextBox is just a bad example. Even if WPF has a lot of "predefined" Commands like the ones in ApplicationCommands. NONE of these do actually anything. Its just to name and give the command a shortcut. It just happens that the TextBox already implement these Commands. And thats what you need to do, Supply a Execute and CanExecute Handler for your desired commands. Check the msdn for CommandBindings. – dowhilefor Feb 27 '12 at 21:37
  • Hey dowhilefor, thanks for the tip, but I'm already familiar with the general commands concept, I just need help for my particular situation which I've never seen before: 1. How do I implement commands when they work on two controls(every example only work on input from one control). I proposed the solution I came up with, but it involves (in)directly referencing a control, which sounds like a bad idea. 2. After solving problem 1, how do I distinguish between two tabs that offer the same functionality? – FZdev Feb 27 '12 at 21:54
  • A command is usually fired on a control with the current focus. So even if you have two textboxes, both handle the Undo command separately, depending on which one the focus has. Is that what you mean? – dowhilefor Feb 27 '12 at 22:02
  • Basically, if two values are not set, the user has NOT entered enough data to run a function in my program. Think of 'reset password' functionality; you MUST enter in your old password and your new password, otherwise, you cannot reset your password. – FZdev Feb 27 '12 at 22:13
  • @FZdevDepending on your ui design, there is a better way in wpf to handle that. Checkout [this](http://blogs.msdn.com/b/vinsibal/archive/2008/08/11/wpf-3-5-sp1-feature-bindinggroups-with-item-level-validation.aspx) to give you an overview of BindingGroups and "per-form" validation – dowhilefor Feb 27 '12 at 23:36

1 Answers1

0

You need to implement CanExecute method that checks both TextBox databinding value and ComboBox.SelectedItem databinding value.

Take example from your question.

Your TextBox.Text should be databinding to your ViewModel And as well as your ComboBox. So your ViewModel should have two Properties:

public string TextBoxCurrentText {get { ...}set {...}}
public string ComboBoxCurrentSelected {get { ...}set {...}}

Then in both Setter, you would do your YourCommmand.RaiseCanExecuteChanged();

So it will execute your CanExecute code piece to determines can your Button be click. Which can be:

    bool YourCommandCanExecute()
    {
//Just example
        if (!string.IsNullOrEmpty(TextBoxCurrentText) && !string.IsNullOrEmpty(ComboBoxCurrentSelected))
            return true;
         return false;
    }
King Chan
  • 4,212
  • 10
  • 46
  • 78
  • I'm not sure that addresses a critical issue: if I write text and set a combobox value in the first tab, but DON'T submit, then go to the second tab and write some more text and DO submit. How would my viewmodel realize that it was the second tab that ran? afterall, the combobox value is still set. – FZdev Feb 27 '12 at 21:52
  • @FZdev I am not sure how you implement your View and ViewModel. If each of the Tab is a View and each View databinding to to different ViewModel, your command that defined in your ViewModel will only check for the databinding value within that ViewModel. i.e View1 databinding to ViewModel1, View2 databinding to ViewModel2. View1 won't affect how ViewModel2 works. – King Chan Feb 27 '12 at 21:58
  • I just define a tabcontrol with two tabitems. in each tabitem, i put in the textbox, button, and (in one case) a combobox with statically defined items. there is one viewmodel with the two properties you inferred(from a previous attempt at a solution, but they are still there and i don't have a problem using them) nothing special really. The multiple tabs is just a specific implementation that I didn't consider when initially designing everything, but I would see like to see what possible solutions are as a learning exercise. – FZdev Feb 27 '12 at 22:01