Binding command to button in silverlight 4 using mvvm

Posted by Archie on Stack Overflow See other posts from Stack Overflow or by Archie
Published on 2010-05-19T07:17:54Z Indexed on 2010/05/19 7:20 UTC
Read the original article Hit count: 277

Hello,

I have a user control called HomePage.xaml. I'm creating a model instance (using MVVM pattern) in the code behind file in the constructor of page as

MainViewModel model = new MainViewModel();

I have a button in HomePage.xaml which I want to bind to the command inside MainViewModel called GetData() and want to populate the data in datagrid. MainViewModel has an ObservableCollection which I would use to bind the data in datagrid.

Populating the data in datagrid without binding command works fine.

I'm binding the button as:

<StackPanel x:Name="stkPanelInput" Orientation="Horizontal" VerticalAlignment="Center" HorizontalAlignment="Center">
<Button x:Name="buttonGetData" Width="70" Content="GetData" Command="{Binding GetData}"  Click="buttonGetData_Click"/>
</StackPanel>

How shall I bind the command using MVVM? Thanks.

© Stack Overflow or respective owner

Related posts about binding

Related posts about button