Saturday, December 8, 2012

datagrid column button command (MVVM)

datagrid column button command (MVVM)

I have the following

<ResourceDictionary   xmlns:vm="clr-namespace:SilverlightApplication1.ViewModels.Controls">   <DataTemplate DataType="vm:CVItemVM" x:Key="viewmodel"> 

..........

                    <sdk:DataGrid Grid.Row="1" AutoGenerateColumns="False" Width="820" MaxColumnWidth="395" ItemsSource="{Binding Path=Publications}" Visibility="{Binding Path=Publications.Count, Converter={StaticResource ConverterVisibility2_1}}">                         <sdk:DataGrid.ColumnHeaderStyle>                             <Style TargetType="sdk:DataGridColumnHeader">                                 <Setter Property="FontWeight" Value="Bold" />                                                                  </Style>                         </sdk:DataGrid.ColumnHeaderStyle>                         <sdk:DataGrid.Columns>                             <sdk:DataGridTextColumn Header="Title" Binding="{Binding Path=Title}"  CanUserResize="False" IsReadOnly="True" />                             <sdk:DataGridTextColumn Header="Author" Binding="{Binding Path=Author}" CanUserResize="False" IsReadOnly="True"  />                             <sdk:DataGridTemplateColumn Header="">                                                                     <sdk:DataGridTemplateColumn.CellTemplate>                                     <DataTemplate>                                         <Button Content="{Binding Path=Id}" Command="{Binding Path=DeleteCommand, Mode=TwoWay}" />                                                                                 </DataTemplate>                                 </sdk:DataGridTemplateColumn.CellTemplate>                             </sdk:DataGridTemplateColumn>                                                         </sdk:DataGrid.Columns>                     </sdk:DataGrid> 

The problem I'm facing is that

<Button Content="{Binding Path=Id}" Command="{Binding Path=DeleteCommand, Mode=TwoWay}" />                                             

Does not trigger the command which is bind to.

ItemsSource="{Binding Path=Publications}" 

refers to the following (in the viewmodel)

public ObservableCollection<PublicationVM> Publications 

The same viewmodel exposes

public ICommand DeleteCommand { get; set; } 

Answers & Comments...

Answer: 1
<Button Content="{Binding Path=Id}" Command="{Binding Path=DeleteCommand, Mode=TwoWay}" /> 

this code trying to access DeleteCommand property from PublicationVM object. if such property defined there, it should work (if property not null ofc.). if DeleteCommand defined in the same VM where

public ObservableCollection<PublicationVM> Publications 

then u should use RelativeSource to your VM, for example:

<Button Content="{Binding Path=Id}" Command="{Binding DataContext.DeleteCommand, RelativeSource={RelativeSource AncestorType=UserControl}}" /> 

where UserControl - type of your control where DataContext = YourViewModel

by : Dmitry Martovoihttp://stackoverflow.com/users/976231




No comments:

Post a Comment

Send us your comment related to the topic mentioned on the blog