DataGridCheckBoxColumn Click Event

Jan 20, 2009 at 11:14 AM

I have a Grid with three columns as below.

                    <dg:DataGridCheckBoxColumn  x:Name="dualInputCbCol"  Binding="{Binding Path=IsChecked}" Width=" auto">                        
                    <dg:DataGridTextColumn x:Name="indexFieldText" Binding="{Binding Path=Name}"   Width=" 157"   />                    
                    <dg:DataGridComboBoxColumn  x:Name ="joinComboBox" Width=" 120"></dg:DataGridComboBoxColumn>                    

I need to Enable the DataGridComboBoxColumn  "joinComboBox" when the DataGridCheckBoxColumn  "dualInputCbCol" is checked and when the DataGridCheckBoxColumn  is unchecked the DataGridComboBoxColumn  should be disabled.

Can anyone help me how to do this?


Jan 21, 2009 at 7:27 AM
Edited Jan 21, 2009 at 7:29 AM
Your IsChecked property must bind with IsEnabled property of the combobox.

For this create a style with target type as combobox, in that style add a setter with property isEnabled and value as "{Binding Path=IsChecked}" and assign the style to the DataGridComboBoxColumn's EditingElementStyle

Jan 21, 2009 at 11:08 AM
Thanks for replying to this discussion.

IsChecked property is a column from a DataTable as below.

DataTable properties = new DataTable();
properties.Columns.Add(new DataColumn("IsChecked"));
properties.Columns.Add(new DataColumn("Name"));

So the IsChecked Column will have true or false values.
What I need to do is when the DataGridCheckBoxColumn  is unchecked I need to Disable the corresponding DataGridComboBoxColumn in the row.

I created a style for the DataGridCheckBoxColumn   as below.

                    <Style x:Key="SingleClickEditing" TargetType="{x:Type dg:DataGridCell}">
                        <EventSetter Event="PreviewMouseLeftButtonDown" Handler="DataGridCell_PreviewMouseLeftButtonDown"></EventSetter>

and in the DataGridCheckBoxColumn   specified the Cellstyle

<dg:DataGridCheckBoxColumn x:Name="dualInputCbCol"
                      CellStyle="{StaticResource SingleClickEditing}" Binding="{Binding Path=IsChecked}" Width=" auto"    >

This is explained in the discussion

My problem now is how should I check if the DataGridCheckBoxColumn is checked in DataGridCell_PreviewMouseLeftButtonDown event in C# code and disable the DataGridComboBoxColumn?

Tried with GetValue() method as below but no luck with it :(

public static readonly DependencyProperty autoGenerateProperty = DependencyProperty.Register("IsChecked",

private void DataGridCell_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
            DataGridCell cell = sender as DataGridCell;
               DataGrid dataGrid = FindVisualParent<DataGrid>(cell);
                if (dataGrid != null)
                    Microsoft.Windows.Controls.DataGridCheckBoxColumn chk = (Microsoft.Windows.Controls.DataGridCheckBoxColumn)dataGrid.FindName("dualInputCbCol");
                    string str = chk.GetValue(autoGenerateProperty).ToString();

Jan 21, 2009 at 1:13 PM
Why are you trying to get the value from the UI when you can get it from the IsChecked property that DataGridCheckBoxColumn is bound to.  I think that's what vinitvsankhe is saying.
Jan 22, 2009 at 3:06 AM
I tried creating a style as below but it does not work.

                            <Style TargetType="ComboBox">
                            <Setter Property="IsEnabled" Value="{Binding Path=IsChecked , Mode=TwoWay}" />
Jan 22, 2009 at 5:20 AM
You can try to forward the datacontext to the column following the steps that Jaime talks about here,  Or you can use a DataGridTemplateColumn using a ComboBox as the DataTemplate.  The binding will automatically forward for the DataGridTemplateColumn.
Jan 22, 2009 at 5:44 AM
Thanks for the reply I will try this out.
One quick qustion. If I am using DataGridTemplateColumn ,
I have a public property
public ArrayList List

I need to give this List as the ItemSource for the below Combobox
<dg:DataGridTemplateColumn  x:Name="comboTemplate" x:Uid="comboTemplate">
                        <dg:DataGridTemplateColumn.CellTemplate x:Uid="comboTemplateColumn">
                            <DataTemplate x:Name="comboDataTemplate">
                                <ComboBox IsEnabled="{Binding Path=IsChecked,Mode=TwoWay}" ItemsSource=List  />

But ItemsSource=List does not work. And also is it possible to give ItemsSource in a seperate ArrayList and use "IsChecked" property in a DataTable for <ComboBox IsEnabled="{Binding Path=IsChecked"}   ...... >??

Jan 22, 2009 at 6:30 AM

I tried creating a style and it worked at last.
The style should be as below
<dg:DataGridComboBoxColumn  x:Name ="joinComboBox" Width=" 120">
                            <Style TargetType="ComboBox">
                                <Setter Property="IsEnabled" Value="{Binding Path=IsChecked,Mode=TwoWay}"/>

Before I have put <dg:DataGridComboBoxColumn.ElementStyle> by mistake.

Thanks a lot vinitvsankhe and vinsibal for the help :)