2
votes

I have a datagrid populated with elements and a checkbox for each element. I'm looking for a way to have an object in my ViewModel be whichever element currently has its checkbox checked.
Here is my XAML so far :

<Window x:Class="fun_with_DataGridCheckBoxColumns.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:fun_with_DataGridCheckBoxColumns"
    mc:Ignorable="d"
    Title="MainWindow" Height="350" Width="525">
<DockPanel>
    <StackPanel Orientation="Horizontal" DockPanel.Dock="Top">
        <Label Content="Chosen One : " />
        <Label Content="{Binding ChosenOne.Name, Mode=OneWay}" />
    </StackPanel>
    <DataGrid ItemsSource="{Binding People}" AutoGenerateColumns="False" CanUserAddRows="False">
        <DataGrid.Columns>
            <DataGridTextColumn Header="ID" Binding="{Binding ID, Mode=OneWay}" IsReadOnly="True"/>
            <DataGridTextColumn Header="Name" Binding="{Binding Name, Mode=OneWay}" IsReadOnly="True"/>
            <DataGridCheckBoxColumn Header="Is Chosen"/>
        </DataGrid.Columns>
    </DataGrid>
</DockPanel>

and my CS :

using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows;

namespace fun_with_DataGridCheckBoxColumns
{
    public partial class MainWindow : Window
    {
        public Person ChosenOne { get; set; }

        public MainWindow()
        {
        InitializeComponent();
        DataContext = new Viewmodel();
        }
    }

    public class Viewmodel : INotifyPropertyChanged
    {
        public ObservableCollection<Person> People { get; private set; }
        private Person _chosenOne = null;
        public Person ChosenOne
        {
            get
            {
                if (_chosenOne == null) { return new Person { Name = "Does Not Exist" }; }
                else return _chosenOne;
            }
            set
            {
                _chosenOne = value;
                NotifyPropertyChanged("ChosenOne");
            }
        }

        public Viewmodel()
        {
            People = new ObservableCollection<Person>
            {
                new Person { Name = "John" },
                new Person { Name = "Marie" },
                new Person { Name = "Bob" },
                new Person { Name = "Sarah" }
            };
        }

        public event PropertyChangedEventHandler PropertyChanged;
        public void NotifyPropertyChanged(string propertyName)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    public class Person
    {
        private static int person_quantity = 0;
        private int _id = ++person_quantity;
        public int ID { get { return _id; } }
        public string Name { get; set; }
    }
}

Here is the behavior I am looking for :

  • ChosenOne in ViewModel becomes whichever Person has its checkbox checked
  • When a checkbox is checked, all others are unchecked
  • If no checkboxes are checked, sets ChosenOne to null

Basically it is the same behavior as if I had put this in the DataGrid (XAML) :

SelectedItem="{Binding ChosenOne, Mode=TwoWay}"

But in my case ChosenOne can not be the SelectedItem of the datagrid since I need SelectedItem for something else, and I have to use checkboxes for Company reasons.

I have not found how to simulate this "SelectedItem" logic with checkboxes.

I know I could put a "bool IsChosen" property in my Person class and bind the checkbox to it, but I would really rather avoid this. It will be my solution if all else fails.

Thank you.

2

2 Answers

0
votes

An alternative would be to wrap your object with something that supports the checking. Source

using System.ComponentModel;

namespace Jarloo
{
    public class CheckedListItem<T> : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        private bool isChecked;
        private T item;

        public CheckedListItem()
        {}

        public CheckedListItem(T item, bool isChecked=false)
        {
            this.item = item;
            this.isChecked = isChecked;
        }

        public T Item
        {
            get { return item; }
            set
            {
                item = value;
                if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("Item"));
            }
        }


        public bool IsChecked
        {
            get { return isChecked; }
            set
            {
                isChecked = value;
                if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("IsChecked"));
            }
        }
    }
}
0
votes

Add an IsChecked property to the Person class and implement the INotifyPropertyChanged interface:

public class Person : INotifyPropertyChanged
{
    private static int person_quantity = 0;
    private int _id = ++person_quantity;
    public int ID { get { return _id; } }
    public string Name { get; set; }

    private bool _isChecked;
    public bool IsChecked
    {
        get { return _isChecked; }
        set { _isChecked = value; NotifyPropertyChanged("IsChecked"); }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    public void NotifyPropertyChanged(string propertyName)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

Bind the DataGridCheckBoxColumn to this property:

<DataGridCheckBoxColumn Header="Is Chosen" Binding="{Binding IsChecked, UpdateSourceTrigger=PropertyChanged}"/>

You can then handle the logic in your view model class. This should make sure that only a single Person is selected at a time:

public class Viewmodel : INotifyPropertyChanged
{
    public ObservableCollection<Person> People { get; private set; }
    private Person _chosenOne = null;
    public Person ChosenOne
    {
        get
        {
            if (_chosenOne == null) { return new Person { Name = "Does Not Exist" }; }
            else return _chosenOne;
        }
        set
        {
            _chosenOne = value;
            NotifyPropertyChanged("ChosenOne");
        }
    }

    public Viewmodel()
    {
        People = new ObservableCollection<Person>
        {
            new Person { Name = "John" },
            new Person { Name = "Marie" },
            new Person { Name = "Bob" },
            new Person { Name = "Sarah" }
        };

        foreach(Person p in People)
            p.PropertyChanged += P_PropertyChanged;
    }

    private bool handle = true;
    private void P_PropertyChanged(object sender, PropertyChangedEventArgs e)
    {
        if(handle && e.PropertyName == "IsChecked")
        {
            handle = false;
            //uncheck all other persons
            foreach (Person p in People)
                if(p != sender)
                    p.IsChecked = false;

            ChosenOne = sender as Person;

            handle = true;
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    public void NotifyPropertyChanged(string propertyName)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

If you plan to add new Person objects to the People collection dynamically at runtime you should also make sure that you handle the PropertyChanged event for these as well:

public Viewmodel()
{
    People = new ObservableCollection<Person>
            {
                new Person { Name = "John" },
                new Person { Name = "Marie" },
                new Person { Name = "Bob" },
                new Person { Name = "Sarah" }
            };

    foreach (Person p in People)
        p.PropertyChanged += P_PropertyChanged;


    People.CollectionChanged += (s, e) =>
    {
        if (e.NewItems != null)
        {
            foreach (object person in e.NewItems)
            {
                (person as INotifyPropertyChanged).PropertyChanged
                    += new PropertyChangedEventHandler(P_PropertyChanged);
            }
        }

        if (e.OldItems != null)
        {
            foreach (object person in e.OldItems)
            {
                (person as INotifyPropertyChanged).PropertyChanged
                    -= new PropertyChangedEventHandler(P_PropertyChanged);
            }
        }
    };

    People.Add(new Person() { Name = "New..." });
}