How to bind data in a DataGridTemplateColumn when an ItemSource is already bound to a DataGrid?

I have a DataGrid that is bound to the following list.

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>

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 parameter to the header and bind it to the DataContext method.

So 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 this function. I think this is because the WorkItem property does not contain the IsAllChecked property . I do not know how to indicate that the binding should be applied to the DataContext instead of the current ItemsSource = "{Binding WorkItems}"

+4
source share
1 answer

, , - .

0