0

I know I can bind a control, say TextBox to a CheckBox value using the following code:

<CheckBox Name="cb1" />
<TextBox IsEnabled="{Binding ElementName=cb1, Path=IsChecked}" />

How can I do the same in a DataGrid ?

I have a DataGrid:

<DataGrid Name="xDataGridName" >
    <DataGrid.Columns>

        <DataGridTemplateColumn Header="Enable">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                   <flatcheckbox:FlatCheckBox x:Name="xDGCheck" Margin="0" IsChecked="{Binding Path=Enabled, Mode=TwoWay}" />
                </DataTemplate>                                              
            </DataGridTemplateColumn.CellTemplate>

            <DataGridTemplateColumn.CellEditingTemplate>
                <DataTemplate>
                    <flatcheckbox:FlatCheckBox x:Name="xDGCheck" Margin="0" IsChecked="{Binding Path=Enabled, Mode=TwoWay}" />
                </DataTemplate>
            </DataGridTemplateColumn.CellEditingTemplate>
        </DataGridTemplateColumn>



        <DataGridTemplateColumn Header="Group">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                   <TextBlock Name="xTextBlockName" Text="{Binding Path=Group}" />
                </DataTemplate>                                              
            </DataGridTemplateColumn.CellTemplate>

            <DataGridTemplateColumn.CellEditingTemplate>
                <DataTemplate>

                    <!-- this is always enabled, i want it to be enable only when checkbox is checked -->    
                    <TextBlock Name="xTextBlockName" Text="{Binding Path=Group}" IsEnabled="{Binding ElementName=xDGCheck, Path=IsChecked}" />
                </DataTemplate>
            </DataGridTemplateColumn.CellEditingTemplate>
        </DataGridTemplateColumn>

    </DataGrid.Columns>
</DataGrid>

How can implement the binding in a DataGrid ?

8
  • What is FlatCheckBox? Can it be replaced with just usual CheckBox, or it's important for the question? Commented Feb 15, 2019 at 10:09
  • You bind to the Enabled source property. Commented Feb 15, 2019 at 10:11
  • And why not just IsEnabled="{Binding Path=Enabled}, if you are anyway propagating the state into the VM? Commented Feb 15, 2019 at 10:11
  • @Vlad FlatCheckBox is just a CheckBox with custom styling applied to it. And I'm binding it to the checkbox value ( and not to Enabled ) because I want the textbox to be enabled/disabled immediately when I check/uncheck the checkbox. Binding it to Enabled, I have to check the checkbox, lose focus of the row, only then the textbox becomes enabled, and vice versa Commented Feb 15, 2019 at 10:17
  • 1
    @mrid I think this is the real problem to solve. Did you try specifying the UpdateSourceTrigger? IsChecked="{Binding Path=Enabled, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Commented Feb 15, 2019 at 10:23

2 Answers 2

2

You should bind to the Enabled source property instead of binding to the IsChecked property of the CheckBox:

<TextBlock Name="xTextBlockName" Text="{Binding Path=Group}" IsEnabled="{Binding Enabled}"/>

The Enabled property of your data class is set by the CheckBox so it should always have the same value as the IsChecked property of the CheckBox.

Make sure that your data object - the class where the Enabled property is defined - implements the INotifyPropertyChanged interface and raise the PropertyChanged event for the Enabled property when it is set by the CheckBox.

As pointed out by @Hyarus, you should also set the UpdateSourceTrigger to PropertyChanged for the source property to get set immediately:

<flatcheckbox:FlatCheckBox x:Name="xDGCheck" Margin="0" IsChecked="{Binding Path=Enabled, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
Sign up to request clarification or add additional context in comments.

4 Comments

@mrid: I don't understand. Your CheckBox is bound to a source property called Enabled, isn't it? If not, it should be.
this is what is happening: drive.google.com/drive/folders/…
Please update your question instead of providing a link to Google Drive. By the way, did you see my last edit?
+1 But i think you need to add the UpdateSourceTrigger=PropertyChanged to the checkbox (or both). It is a problem of the checkbox not updating the property, not the textblock getting the update.
0

As you already having Enabled property binded with your CheckBox, you can use the same to bind it with IsEnabled property of your TextBox. Your CheckBox binding should've UpdateSourceTrigger set to PropertyChanged Working sample below,

XAML

<DataGrid ItemsSource="{Binding Items}" AutoGenerateColumns="False">
    <DataGrid.Columns>
        <DataGridTemplateColumn Header="Enable">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <CheckBox IsChecked="{Binding Path=Enabled, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>

        <DataGridTemplateColumn Header="Group">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Path=Group}" />
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>

            <DataGridTemplateColumn.CellEditingTemplate>
                <DataTemplate>
                    <TextBox Text="{Binding Path=Group}" IsEnabled="{Binding Path=Enabled, Mode=TwoWay}" />
                </DataTemplate>
            </DataGridTemplateColumn.CellEditingTemplate>
        </DataGridTemplateColumn>
    </DataGrid.Columns>
</DataGrid>

Model

public class Model : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    private bool _enabled;
    private string _group;

    public bool Enabled
    {
        get => _enabled; set
        {
            _enabled = value;
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Enabled)));
        }
    }

    public string Group
    {
        get { return _group; }
        set
        {
            _group = value;
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Group)));
        }
    }
}

ViewModel

public class ViewModel
{
    public ObservableCollection<Model> Items { get; set; }
    public ViewModel()
    {
        Items = new ObservableCollection<Model>
        {
            {new Model{ Enabled = true, Group = "Group1"} },
            {new Model{ Enabled = false, Group = "Group2"} },
            {new Model{ Enabled = false, Group = "Group3"} },
            {new Model{ Enabled = true, Group = "Group4"} },
            {new Model{ Enabled = false, Group = "Group5"} },
            {new Model{ Enabled = false, Group = "Group6"} }
        };
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.