1

I have simplified example:

XAML:

<CheckBox IsChecked="{Binding Path=IsSelected, Mode=TwoWay}" Name="cb" />
<Button Name="button1" Click="button1_Click" />

Code behind:

public partial class MainWindow : Window
{
    private ObservableCollection<MyObject> collection = new ObservableCollection<MyObject>();

    public MainWindow()
    {
        InitializeComponent();

        collection.Add(new MyObject(true));
        //grid.DataContext = collection[0];
    }

    private void button1_Click(object sender, RoutedEventArgs e)
    {
        collection[0].IsSelected = false;
    }
}

public class MyObject
{
    public bool IsSelected { get; set; }

    public MyObject(bool isSelected)
    {
        this.IsSelected = isSelected;
    }
}

The cb.IsChecked doesn't change by button clicking though the collection[0].IsSelected is changed.

Even if I uncomment grid.DataContext = collection[0]; - nothing changed.

In real example I have the same checkbox in the item template of a listbox. So the behaviour is the same - the selection of checkboxes don't change.

2 Answers 2

3

You need to implement INotifyPropertyChanged on your MyObject type

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

Comments

2

Please try the following codes:

public class MyObject : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    private void NotifyPropertyChanged(string info)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(info));
        }
    }

    private bool _isSelected;

    public bool IsSelected
    {
        get { return _isSelected; }
        set
        {
            _isSelected = value;
            NotifyPropertyChanged("IsSelected");
        }
    }

    public MyObject(bool isSelected)
    {
        this.IsSelected = isSelected;
    }
}

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.