mercredi 25 janvier 2017

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!




Aucun commentaire:

Enregistrer un commentaire