Checking Select All checkbox in WPF does not change other checkboxes in Listbox

I have implemented ListBox

using this and this . I am linking my actual list of 29 objects 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 is:

I want to have select-all CheckBoxes

so that the user can select and deselect all items. It works weird!

Once checked, selectAll CheckBox

all items will be checked that are not in the scrollbar area (the list is scrolling), then I have to 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 do not know what to do.

Thanks in advance, Mo

+3


source to share


1 answer


Your implementation IsChecked

might look like this.

public class Datenpunkt : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    public void Notify(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    private bool _isChecked;
    public bool IsChecked
    {
        get { return _isChecked; }
        set
        {
            _isChecked = value;
            Notify("IsChecked");
        }
    }
}

      



Check out the MSDN INotifyPropertyChanged page for more information .

+2


source







All Articles