locked
universal MVVM : Usercontrol using MVVM

    General discussion

  • Hi,

    I am doing RND on MVVM for my new project. I am new in MVVM. My question is .

    " I have 4 Buttons , Each button has there own functionality. For example I have Button1 , Button2, Button3 and Button4. Button1 redirect me on Page1

    Button2 redirect me on Page2

    Button3 redirect me on Page3

    Button4 redirect me on Page4 .

    Before redirect to next page there is a selection popup. The items of popup selection is same for all so I have created a usercontrol for it. So Button1 click then open a selection popup , user will select some data then redirect to Page1. All Buttons do same things.

    I want to do this things with help of MVVM So can any one give guide me how to achieve this thing. "


    shah


    Hi also chekc these links please Passing parameters to View-Model from View with command.
    • Edited by MauliKShah Monday, January 12, 2015 11:54 AM
    Monday, January 12, 2015 11:47 AM

All replies

  • Hi,

    You can bind the Command property of the button to any property that returns ICommand. Prism implements a nice convenient command called DelegateCommand that is very easy to use:

    public ICommand MyButtonClickCommand
            {
                get { return new DelegateCommand<object>(FuncToCall, FuncToEvaluate); }
            }
    
            private void FuncToCall(object context)
            {
                //this is called when the button is clicked
            }
    
            private bool FuncToEvaluate(object context)
            {
                //this is called to evaluate whether FuncToCall can be called
                //for example you can return true or false based on some validation logic
                return true;
            }

    And according to your description, I recommend you can bind all buttons command to the MyButtonClickCommand, and use the the CommandParameter property to differentiate these buttons.  And the type of the expected argument which define in CommandParameter is specified in the FuncToCall and FuncToEvaluate target methods. So you can navigate to the different page base on the different argument:

    <Button Content="Button" Margin="810,393,0,337"  Command="{Binding MyButtonClickCommand}" CommandParameter="button1" />
    <Button Content="Button" Margin="500,300,0,400"  Command="{Binding MyButtonClickCommand}" CommandParameter="button2"/>
    Best Wishes!

    Tuesday, January 13, 2015 9:36 AM