Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Click event for DataGridCheckBoxColumn

Tags:

I have a DataGrid in a WPF form with a DataGridCheckBoxColumn, but I did not find any click event, Checked and unchecked for it...

Are these events available for the DataGridCheckBoxColumn? If not please suggest some workaround I could use.

like image 955
Prashant Cholachagudda Avatar asked Apr 17 '09 09:04

Prashant Cholachagudda


2 Answers

Quoted from William Han's answer here: http://social.msdn.microsoft.com/Forums/ar/wpf/thread/9e3cb8bc-a860-44e7-b4da-5c8b8d40126d

It simply adds an event to the column. It is a good simple solution.

Perhaps you can use EventSetter as example below:

Markup:

<Window x:Class="DataGridCheckBoxColumnTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:DataGridCheckBoxColumnTest"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <local:People x:Key="People"/>
    </Window.Resources>
    <Grid>
        <DataGrid ItemsSource="{StaticResource People}" AutoGenerateColumns="False">
            <DataGrid.Columns>
                <DataGridTextColumn Binding="{Binding Path=Name}" Header="Name"/>
                <DataGridCheckBoxColumn Binding="{Binding Path=LikeCar}" Header="LikeCar">
                    <DataGridCheckBoxColumn.CellStyle>
                        <Style>
                            <EventSetter Event="CheckBox.Checked" Handler="OnChecked"/>
                        </Style>
                    </DataGridCheckBoxColumn.CellStyle>
                </DataGridCheckBoxColumn>
            </DataGrid.Columns>
        </DataGrid>
    </Grid>
</Window>

Code:

using System;
using System.Windows;

namespace DataGridCheckBoxColumnTest
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        void OnChecked(object sender, RoutedEventArgs e)
        {
            throw new NotImplementedException();
        }
    }
}


namespace DataGridCheckBoxColumnTest
{
    public class Person
    {
        public Person(string name, bool likeCar)
        {
            Name = name;
            LikeCar = likeCar;
        }
        public string Name { set; get; }
        public bool LikeCar { set; get; }
    }
}

using System.Collections.Generic;

namespace DataGridCheckBoxColumnTest
{
    public class People : List<Person>
    {
        public People()
        {
            Add(new Person("Tom", false));
            Add(new Person("Jen", false));
        }
    }
}
like image 187
IFink Avatar answered Sep 19 '22 20:09

IFink


Expanding on the DataGridCell concept noted above, this is what we used to get it working.

...XAML...

    <DataGrid Grid.ColumnSpan="2" Name="dgMissingNames" ItemsSource="{Binding Path=TheMissingChildren}" Style="{StaticResource NameListGrid}" SelectionChanged="DataGrid_SelectionChanged">
        <DataGrid.Columns>
            <DataGridTemplateColumn CellStyle="{StaticResource NameListCol}">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <CheckBox IsChecked="{Binding Path=Checked, UpdateSourceTrigger=PropertyChanged}" Name="theCheckbox" />
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>                            
            </DataGridTemplateColumn>
            <DataGridTextColumn Binding="{Binding Path=SKU}" Header="Album" />
            <DataGridTextColumn Binding="{Binding Path=Name}" Header="Name" "/>
            <DataGridTextColumn Binding="{Binding Path=Pronunciation}" Header="Pronunciation" />
        </DataGrid.Columns>
    </DataGrid>

TheMissingChildren is an ObservableCollection object that contains the list of data elements including a boolean field "Checked" that we use to populate the datagrid.

The SelectionChanged code here will set the checked boolean in the underlying TheMissingChildren object and fire off a refresh of the items list. That ensures that the box will get checked off & display the new state no matter where you click on the row. Clicking the checkbox or somewhere in the row will toggle the check on/off.

private void DataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    DataGrid ThisGrid = (DataGrid)sender;
    CheckedMusicFile ThisMusicfile = (CheckedMusicFile)ThisGrid.SelectedItem;
    ThisMusicfile.Checked = !ThisMusicfile.Checked;
    ThisGrid.Items.Refresh();
}
like image 37
Lance Cleveland Avatar answered Sep 20 '22 20:09

Lance Cleveland