Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

Select all checkbox in wpf datagrid

$
0
0

Hi everyone

I have a problem. I have a datagrid and in it I needed to add a checkbox column with a deselect all checkbox on top that is bound to a property in my class object that is of type int and can be 1 and 0.

I have no idea how to even go about doing this. I have tried many things so far.

Here is the wpf xaml for the datagrid column

<DataGridTemplateColumn><DataGridTemplateColumn.HeaderTemplate><DataTemplate><CheckBox x:Name="all" Content="Select All" Click="CheckBox_Click" IsChecked="{Binding IsChecked}"/></DataTemplate></DataGridTemplateColumn.HeaderTemplate><DataGridTemplateColumn.CellTemplate><DataTemplate><CheckBox Content="Aktivan" Click="CheckBox_Checked" IsChecked="{Binding ElementName=all, Path=IsChecked}" ></CheckBox></DataTemplate></DataGridTemplateColumn.CellTemplate></DataGridTemplateColumn>

And here is the Checkbox click method I tried

 private void CheckBox_Click(object sender, RoutedEventArgs e)
        {
            var checkBox = sender as CheckBox;
            if (null == checkBox) return;

            if (checkBox.IsChecked == true) 
            {
            checkBox.IsChecked = true;
            dg.Items.Refresh();
            }
            else 
            {
                checkBox.IsChecked=false;
                dg.Items.Refresh();
                }
            
        }

It did not work obviously. I also tried doing something different with the CheckBox_Checked method I found on the internet

private void CheckBox_Checked(object sender, RoutedEventArgs e)
        {
            Zastupnik zas = dg.SelectedItem as Zastupnik;
            var checkBox = sender as CheckBox;
            if (null == checkBox) return;
            foreach (var Zastupnik in  this.Presenter.Zastupnici)
                {
                if (Zastupnik.Aktivan==1)
                    {
                        Zastupnik.IsChecked = true;
                         }
                else
                {
                    Zastupnik.IsChecked = false;
                }
                Zastupnik.IsChecked = checkBox.IsChecked.Value;
                }
        }

This did not work either. Zastupnik is my object and the 2 properties, Aktivan is the int and IsChecked is a new property I added of type bool.

I have searched through the internet and found a lot of questions like this but none of the solutions there worked for me (probably because I didn't know how to implement them correctly). I found a tree view tutorial for it which looks good but I was unable to get it well enough to implement.

I also wanna say that the Checked, Unchecked events are a bit buggy so I would like to avoid them as I have found them to fire when I scroll. This is why I use Click.

I really could use all the help I can get here guys as I am just stuck.


Viewing all articles
Browse latest Browse all 18858

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>