DataGridCheckBoxColumn Click Event

Jan 20, 2009 at 12:14 PM
Hi,

I have a Grid with three columns as below.

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

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?

kanchana


Jan 21, 2009 at 8:27 AM
Edited Jan 21, 2009 at 8: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 12:08 PM
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.

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

and in the DataGridCheckBoxColumn   specified the Cellstyle

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

This is explained in the discussion http://www.codeplex.com/wpf/Wiki/View.aspx?title=Single-Click%20Editing

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",
                                                                                                  typeof(bool),
                                                                                                  typeof(DataTable));


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();
                }
        }


Coordinator
Jan 21, 2009 at 2: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 4:06 AM
I tried creating a style as below but it does not work.

<dg:DataGridComboBoxColumn.ElementStyle>
                            <Style TargetType="ComboBox">
                            <Setter Property="IsEnabled" Value="{Binding Path=IsChecked , Mode=TwoWay}" />
                            </Style>
</dg:DataGridComboBoxColumn.ElementStyle>
Coordinator
Jan 22, 2009 at 6:20 AM
You can try to forward the datacontext to the column following the steps that Jaime talks about here, http://blogs.msdn.com/jaimer/archive/2008/11/22/forwarding-the-datagrid-s-datacontext-to-its-columns.aspx.  Or you can use a DataGridTemplateColumn using a ComboBox as the DataTemplate.  The binding will automatically forward for the DataGridTemplateColumn.
Jan 22, 2009 at 6: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
{
get;set;
}

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  />
                            </DataTemplate>
                        </dg:DataGridTemplateColumn.CellTemplate>
</dg:DataGridTemplateColumn>

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 7:30 AM
Hi

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

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

Thanks a lot vinitvsankhe and vinsibal for the help :)