I have a DataGrid which is bound to a list as follow.
The View:
<DataGrid Name="m_dgWorkItems" ItemsSource="{Binding WorkItems}" AutoGenerateColumns="False" CanUserAddRows="False" Margin="12,70,12,12">
<DataGrid.Columns>
<DataGridCheckBoxColumn Header="Selected" Binding="{Binding Selected}" />
<DataGridTextColumn Header="Id" Binding="{Binding Id}" IsReadOnly="True" />
<DataGridTextColumn Header="Summary" Binding="{Binding Summary}" IsReadOnly="True" />
<DataGridTextColumn Header="State" Binding="{Binding State}" IsReadOnly="True" />
<DataGridTextColumn Header="Owner" Binding="{Binding Owner}" IsReadOnly="True" />
</DataGrid.Columns>
</DataGrid>
The ViewModel:
public List<WorkItem> WorkItems
{
get { return m_workItems; }
private set { m_workItems = value; RaisePropertyChanged("WorkItems"); }
}
public class WorkItem
{
public bool Selected { get; set; }
public string Id { get; set; }
public string Summary { get; set; }
public string State { get; set; }
public string Owner { get; set; }
public string Description { get; set; }
}
I want to add a (Un)SelectAll option to the header and bind it to a method of the DataContext.
Therefore, I changed the View:
<DataGrid Name="m_dgWorkItems" ItemsSource="{Binding WorkItems}" AutoGenerateColumns="False" CanUserAddRows="False" Margin="12,70,12,12">
<DataGrid.Columns>
<!--<DataGridCheckBoxColumn Header="Selected" Binding="{Binding Selected}" />-->
<DataGridTemplateColumn>
<DataGridTemplateColumn.Header >
<CheckBox Content="Selected" IsChecked="{Binding IsAllChecked, Mode=TwoWay}"></CheckBox>
</DataGridTemplateColumn.Header>
<DataGridTemplateColumn.CellTemplate >
<DataTemplate>
<CheckBox IsChecked="{Binding Selected}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn Header="Id" Binding="{Binding Id}" IsReadOnly="True" />
<DataGridTextColumn Header="Summary" Binding="{Binding Summary}" IsReadOnly="True" />
<DataGridTextColumn Header="State" Binding="{Binding State}" IsReadOnly="True" />
<DataGridTextColumn Header="Owner" Binding="{Binding Owner}" IsReadOnly="True" />
</DataGrid.Columns>
</DataGrid>
And I added this function to the ViewModel:
public bool IsAllChecked
{
get { return m_bIsAllChecked; }
set
{
m_bIsAllChecked = value;
foreach (WorkItem workItem in m_workItems)
workItem.Selected = value;
RaisePropertyChanged("IsAllChecked");
}
}
However, the program never enters into that function. I think that it is due to that the WorkItem property does not contain a IsAllChecked property. I do not know how to specify that the binding should be apply on the DataContext instead of the current ItemsSource="{Binding WorkItems}"