0

I have a simple checkbox items and when items are selected, it works fine. I put a button to unselect all selected items . In the debug mode, I can see the checked state being set to unchecked (false) although it is not reflected in the UI. Here is the code:

XAML for Listbox-Checkbox:

<ListBox x:Name="Listitems"  Grid.Column="0" SelectionMode="Multiple"  ItemsSource="{Binding MonthlyResults}" >
  <ListBox.ItemTemplate>
      <DataTemplate>
       <CheckBox  Content="{Binding logdate}" IsChecked="{Binding Checked ,Mode=TwoWay}" Click="CheckBox_Click"/>
        </DataTemplate>
        </ListBox.ItemTemplate>
       </ListBox>

XAML for UncheckALL button:

<Button Grid.Row="0" Name="ClearALL" Margin="4,10,4,75" Content="Unselect All" FontFamily="Tahoma" FontSize="12" Click="Button_Click"/>

Code behind:

 private void CheckBox_Click(object sender, RoutedEventArgs e)
            {
                var cb = sender as CheckBox;
                var item = cb.DataContext;
                Listitems.SelectedItem = item;
                HornerPlotPluginModel model = DataContext as HornerPlotPluginModel;
                var checkedItems1 = model.MonthlyResults.Where(B => B.Checked == true);  
//monthlyresults is the observable collection that populates the checkbox items
                model.CDFResults.Clear(); // some function
                Chart1.Series.Clear();     
                Chart1.Axes.Clear();
                model.DisplayLogs();   // some function
                DrawCurves();          // some function
            }

Code behind for the UncheckAll button:

private void Button_Click(object sender, RoutedEventArgs e)
        {
            HornerPlotPluginModel model = DataContext as HornerPlotPluginModel;
            var checkedItems1 = model.MonthlyResults.Where(B => B.Checked == true);
            Listitems.SelectedItems.Clear();  //SET CHECKED ITEMS TO FALSE!!!
            model.CDFResults.Clear();
            Chart1.Series.Clear();

        }

I did look at similar post here: WPF UserControl property change not updating but it went over my head!

2 Answers 2

1

Make sure that the class where the Checked property is defined implements the INotifyPropertyChanged interface and raises the PropertyChanged event in the setter of the Checked property:

public class MonthlyReport : INotifyPropertyChanged
{
    private bool _checked;

    public bool Checked
    {
        get { return _checked; }
        set { _checked = value; NotifyPropertyChanged(); }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
}

Then you should be able to simply set the Checked property of all those objects to false to refresh the CheckBox:

HornerPlotPluginModel model = DataContext as HornerPlotPluginModel;
foreach(var item in model.MonthlyResults)
{
    item.Checked = false;
}
Sign up to request clarification or add additional context in comments.

Comments

0
HornerPlotPluginModel model = DataContext as HornerPlotPluginModel;
foreach(var item in model.MonthlyResults)
{
    item.Checked = false;
}

2 Comments

It clears the observable collection but does not refresh the UI. I had tried the same earlier.
Are you using UpdateSourceTrigger="PropertyChanged"?

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.