0

I have a DataGrid with a Checkbox & other Textbox.

   <DataGrid AutoGenerateColumns="False" Height="170" Name="dataGrid1" Width="527"  OpacityMask="#FF161A1A" BorderBrush="#FFB7B39D" Background="LightYellow" RowBackground="LightGray" AlternatingRowBackground="#FFFFFFF5" BorderThickness="10" CanUserResizeRows="False" CanUserReorderColumns="False" CanUserResizeColumns="True" CanUserSortColumns="False" FontFamily="Segoe UI" FontSize="13" CanUserAddRows="False">

       <DataGrid.Columns>
            <DataGridCheckBoxColumn Header="" Binding="{Binding BoolProperty, Mode=TwoWay}" />
            <DataGridTextColumn Header="" Binding="{Binding header}" MinWidth="108" IsReadOnly="True" />
            <DataGridTextColumn Header="Number of Cases" Binding="{Binding cases}" >
            <DataGridTextColumn.EditingElementStyle>
                  <Style TargetType="TextBox">
                        <Setter Property="IsEnabled" Value="{Binding Path=BoolProperty, Mode=TwoWay}" />
                 </Style>
           </DataGridTextColumn.EditingElementStyle>
          </DataGridTextColumn>

The checkboxcolumn is bind to a "BoolProperty". I want is Textbox "Number of Cases" to be disabled if the BoolProperty is false and enable if the BoolProperty is true. I tried adding the IsEnabled in TExtBox, but it doesn't work. Where am I going wrong ?

Mike Kinghan
  • 55,740
  • 12
  • 153
  • 182
Tvd
  • 4,463
  • 18
  • 79
  • 125

2 Answers2

1

For an XAML only approach, use a template column instead. IsReadOnly isn't bindable at the cell level. Since that link doesn't provide implementation, I will.

<DataGridTemplateColumn>
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Path=myProperty}" />
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
    <DataGridTemplateColumn.CellEditingTemplate>
        <DataTemplate>
            <TextBox IsEnabled="{Binding Path=myBool}" Text="{Binding Path=myProperty, Mode=TwoWay}" />
        </DataTemplate>
    </DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>
Community
  • 1
  • 1
jamesSampica
  • 12,230
  • 3
  • 63
  • 85
  • This makes no difference. Regardless of the checkbox is selected or not, I am able to enter value in Number of Cases textbox. – Tvd Aug 26 '13 at 14:06
  • @Tvd You're right, updated with what does work – jamesSampica Aug 26 '13 at 14:30
  • Sorry to ask this, But where do I add this code - I mean it goes between TextBoxCol, or before DataCols or where ? I am not efficient in this matter. Thanks. – Tvd Aug 26 '13 at 14:51
  • `DataGridTemplateColumn` is its own column, like `DataGridTextColumn` – jamesSampica Aug 26 '13 at 14:52
  • it is working, but after selecting checkbox, I got to click 3 times to textbox then can edit it. Even if I unselect the checkbox, then also am able to edit the textbox. Why this weird behavior ? – Tvd Aug 26 '13 at 15:17
  • I accidently switched `CellTemplate` and `CellEditingTemplate`. See updated code – jamesSampica Aug 26 '13 at 15:27
  • No Jim, same thing editable on 3 clicks Plus I also get "Two-way binding requires Path or XPath." error on going to a previously edited textbox. – Tvd Aug 26 '13 at 17:45
  • @Tvd it sounds like you have something else coming into play here. This works perfectly for me on .net 4.5 – jamesSampica Aug 26 '13 at 18:02
  • @Tvd Theres a missing comma after `Path=myProperty` on the `Text` for `TextBox`. I added it now – jamesSampica Aug 26 '13 at 18:09
0

I used the LoadingRow event of my DataGrid in one project to check specific status. Maybe something like this could help:

void dataGrid_LoadingRow(object sender, DataGridRowEventArgs e)
{
    checkRow(e.Row);
}

private void checkRow(DataGridRow dgRow)
{
    if (dgRow == null)
        return;

    var item = dgRow.Item as MyItemClass;
    if (item != null && item.BoolProperty)
    {
        ...
    }
    else
    {
        ...
    }
}

In your case you can enable/disable your cell in the if-else contruct.

Hope it helps.

Christian St.
  • 1,751
  • 2
  • 22
  • 41
  • then what if when user selects/de-selects the checkbox at that time I don't think LoadingRow event will be executed. !!!! – Tvd Aug 26 '13 at 14:07
  • That's right, then you can add a handler to the `CellEditEnding` event of your `DataGrid`, which calls the `checkRow()` function, too. – Christian St. Aug 26 '13 at 17:07