Checking a select-all checkbox in WPF doesn't change other checkboxes in Listbox

Viewed 922

I implemented a ListBox using this and this. I bind my actual list of 29 objects to it and it works well. In XAML:

<ListBox Name="WBauNrList" ItemsSource="{Binding}"  Grid.Row="7" Grid.Column="2" ScrollViewer.VerticalScrollBarVisibility="Visible" ScrollViewer.CanContentScroll="True" Height="100" >
    <ListBox.ItemTemplate>
        <HierarchicalDataTemplate>
            <CheckBox Content="{Binding Baunr}" IsChecked="{Binding IsChecked,Mode=TwoWay}"/>
        </HierarchicalDataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

In code:

datenpunktList = new ObservableCollection<Datenpunkt>();
foreach (var d in WerkstattList.DistinctBy(p => p.lokNr))
{
    var newd = new Datenpunkt() { Baunr = d.lokNr };
    datenpunktList.Add(newd);
}

WBauNrList.ItemsSource = datenpunktList;

But the problem:

I want to have a select-all CheckBoxes to able the user to select and deselect all items. It works strangely!

After checking the selectAll CheckBox, all items will be checked that is not in the scope of the scrollbar (the list is scrolled), then I should scroll down and up to see that all items are checked.

XAML:

<CheckBox Name="selectAll" Click="selectAll_Click" >Secelct all</CheckBox>

Code:

private void selectAll_Click(object sender, RoutedEventArgs e)
{
    foreach (Datenpunkt item in WBauNrList.Items)
    {
        item.IsChecked = true ;
    }
}

I don't have any idea what to do.

Thanks in advance, Mo

1 Answers
Related