3

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

4
  • Have you implemented the INotifyPropertyChanged interface for the IsCheckedproperty of your Datenpunkt class? Commented Jul 11, 2017 at 13:09
  • @MightyBadaboom no. should it look like this? public static readonly DependencyProperty isCheckedProperty = DependencyProperty.Register("IsChecked", typeof(bool), typeof(MainWindow), new UIPropertyMetadata(null)); Commented Jul 11, 2017 at 13:12
  • 1
    If you are binding a property to your xaml and you want your ui to be updated when the property is changed via code you have to implement the INotifyPropertyChanged interface. No, that would be a dependency property. Commented Jul 11, 2017 at 13:13
  • @MightyBadaboom . i will check it now Commented Jul 11, 2017 at 13:30

1 Answer 1

3

Your property IsChecked implementation could 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");
        }
    }
}

Have a look at the MSDN INotifyPropertyChanged page for more information.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.