user7157732
user7157732

Reputation: 347

Setting checkbox selected items to False in User interface - C# WPF

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!

Upvotes: 0

Views: 637

Answers (2)

mm8
mm8

Reputation: 169190

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;
}

Upvotes: 1

Dakota Kronberger
Dakota Kronberger

Reputation: 119

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

Upvotes: 0

Related Questions