locked
Universal MVVM : Passing parameters to View-Model from View with command.

    Question

  • Hi ,

    I am new in MVVM. In normal way (Without MVVM) I can write xaml like as below where I have added two button and fire same event for both button when they are clicked.

    <Button x:Name="bt1" Content="Button1" HorizontalAlignment="Left" VerticalAlignment="Top" Click="ButtonClick"/>
    <Button x:Name="bt2" Content="Button2" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="112,0,0,0" Click="ButtonClick"/>

    in c#

     private void ButtonClick(object sender, RoutedEventArgs e)
            {
                Button bt = (Button)sender;
                Debug.WriteLine("Sender Name : " + bt.Name + " and sender text : " + bt.Content);
            }

    So, Basically in code behind I get sender object so I can identify which button has called. Same logic I want to implement in MVVM here is my xaml code for it

     xmlns:Triggers="using:WinRT.Triggers"
         DataContext="{Binding VM, Source={StaticResource Locator}}"
    
    
     <Grid Background="#FFBD7C7C">
    
            <Button Content="MVVMButton1" HorizontalAlignment="Left" Margin="0,82,0,0" VerticalAlignment="Top">
                <Triggers:Interactions.Triggers>
                    <Triggers:EventTrigger EventName="Click">
                        <Triggers:InvokeCommandAction Command="{Binding ClickedCommand}" />
                    </Triggers:EventTrigger>
                </Triggers:Interactions.Triggers>
            </Button>
            <Button Content="MVVMButton2" HorizontalAlignment="Left" Margin="131,82,0,0" VerticalAlignment="Top">
                <Triggers:Interactions.Triggers>
                    <Triggers:EventTrigger EventName="Click">
                        <Triggers:InvokeCommandAction Command="{Binding ClickedCommand}" />
                    </Triggers:EventTrigger>
                </Triggers:Interactions.Triggers>
            </Button>
    </Grid>


     public ICommand ClickedCommand{ get; private set; }
     public ClassViewModel(IDataService dataService)
    {
    ClickedCommand= new RelayCommand(_clickedcommand);
    }
    
    public void _clickedcommand()
    {
    //How can I get Sender here ?
    }

    So how can I get sender in View- Model.?

    Thank You,



    shah


    Hi also please check thes also Creating Controls at Run Time in XAML(View) , how can i call webview.navigate method from view model , Usercontrol using MVVM ,


    • Edited by MauliKShah Monday, January 12, 2015 11:54 AM
    Monday, January 12, 2015 11:24 AM

Answers

  • its better to have commands that a real meaning. Like ICommand SaveCommand {get;} in your viewmodel that saves some data and ICommand DeleteCommand {get;} that deletes something. Then you can bind each button to a seperate command. If you going to send the sender to the viewmodel your viewmodel gets depending on your view. Something we try to avoid with mvvm

    Microsoft Certified Solutions Developer - Windows Store Apps Using C#

    Tuesday, January 13, 2015 1:26 PM

All replies

  • why need the sender?

    Microsoft Certified Solutions Developer - Windows Store Apps Using C#

    Monday, January 12, 2015 10:12 PM
  • Sender means I just want to get which button has called this. Whether it is Button1 or Button2 so I can write code according to it. i.e. for button1 I want to show popup message and for button2 just redirect to next page.

    shah

    Tuesday, January 13, 2015 3:42 AM
  • Maybe this link can help you understand Event handling using MVVM.

    http://stackoverflow.com/questions/21123096/event-handling-in-windows-store-apps-using-mvvm.

    Tuesday, January 13, 2015 6:39 AM
  • its better to have commands that a real meaning. Like ICommand SaveCommand {get;} in your viewmodel that saves some data and ICommand DeleteCommand {get;} that deletes something. Then you can bind each button to a seperate command. If you going to send the sender to the viewmodel your viewmodel gets depending on your view. Something we try to avoid with mvvm

    Microsoft Certified Solutions Developer - Windows Store Apps Using C#

    Tuesday, January 13, 2015 1:26 PM