How to bind Checkbox's IsEnabled property to GridView's IsReadOnly Property

4 posts, 0 answers
  1. xp
    xp avatar
    13 posts
    Member since:
    Apr 2009

    Posted 31 Jul 2009 Link to this post

    <telerikGrid:RadGridView Name="MyDataGrid"  ItemsSource="{Binding Path=MyDataCollection, Mode=TwoWay}" IsReadOnly="False" AutoGenerateColumns="False" >
                               <telerikGrid:GridViewDataColumn HeaderText="Is Active" DataMemberPath="IsActive">
                                    <Style TargetType="telerikGridView:GridViewCell">
                                        <Setter Property="Template">
                                                <ControlTemplate TargetType="telerikGridView:GridViewCell">
                                                    <Border BorderThickness="{TemplateBinding BorderThickness}"
                                                    BorderBrush="{TemplateBinding BorderBrush}"
                                                    Background="{TemplateBinding Background}">
                                                        <CheckBox IsChecked="{Binding Path=IsActive}" IsEnabled="{Binding Path=IsReadOnly, ElementName=MyDataGrid}" />

    the IsEnabled binding in checkbox seems not working. How can bind it so checkbox will be disabled when gridview's IsReadOnly set to true and enable the checkbox when gridview's IsReadOnly set to false from code behind.

  2. Rossen Hristov
    Rossen Hristov avatar
    2478 posts

    Posted 06 Aug 2009 Link to this post

    Hello sam,

    Since IsReadOnly is not a dependency property that will not work I have prepared a sample project that employs a BindingHelper to overcome this limitiation. You will bind the IsEnabled to the BindingHelper.Value. Then each time you change the IsReadOnly from the code behind you will assign the opposite value to the BindingHelper.Value and since it is a INotifyPropertyChanged the IsEnabled will be updated as well. Please check out the sample project that I have attached. I hope it helps.

    Kind regards,
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
  3. Victor Manuel Gonzalez Tellez
    Victor Manuel Gonzalez Tellez avatar
    7 posts
    Member since:
    Jan 2010

    Posted 15 Jan 2010 Link to this post

    Hi good day., I've used your example because I need a similar thing, and it works,. except for a little detail. This is my case. I have instead of the checkboxes, a textbox. I have an item collection, with a field name, description and quantity.

    The behavior of the grid, must be that in items that already has been saved to the database, the field name cannot be modified, only description and quantity. The user can add new rows (items) and in those the name can be changed. I have binded the grid to MyItemCollection, and I have MyCurrentItem as my selected item, Each of my items have field where it specifies if it is already exists, so when I change the row, it searchs that value, and enables or disables the first row. Those steps work as desire, when you select a new one, the texbox is enabled again. The problem is that when I do a click in the textbox inside the cell, the value SelectedItem is not updated, as a matter of fact, I can have no selected item, and write something in the textbox. So I don't have idea of how correct this behavior. Just as comment, I'm using MVMV and really wouldn't as possible, to use code-behind. I had to make some changes to the BindingHelper in you class to make it work so the viewmodel doesn't have to know anything about the view. Thanks in advance.

     <telerikGridView:RadGridView x:Name="gridSourceInventoryItems"  Background="{x:Null}"  
                                             BorderThickness="0" AutoGenerateColumns="False" ShowGroupPanel="False"  
                                             ShowColumnHeaders="True" VerticalGridlinesThickness="0" Width="Auto" 
                                             GridLinesVisibility="Both"  FontSize="8.5" Margin="0,0,0,0" 
                                             Height="200" ScrollMode="RealTime" IsFilteringAllowed="False" ValidationMode="Cell"  
                                             ItemsSource="{Binding CurrentSourceInventory.SourceInventoryItems, Mode=TwoWay}"  
                                             SelectedItem="{Binding CurrentSourceInventory.CurrentItem, Mode=TwoWay}" 
                                                <telerikGridView:GridViewDataColumn Header="Item" DataMemberPath="ItemValidator" Width="82"
                                                        <Style TargetType="GridView:GridViewCell"
                                                            <Setter Property="Template"
                                                                    <ControlTemplate TargetType="GridView:GridViewCell"
                                                                            <TextBox IsEnabled="{Binding Value, Source={StaticResource bindingHelper}}" 
                                                                            Text="{Binding ItemValidator, Mode=TwoWay}" /> 
                                                <telerikGridView:GridViewDataColumn Header="Qty Spe" DataMemberPath="QtySpecifiedValidator" Width="50"/> 
                                                <telerikGridView:GridViewDataColumn Header="Qty Rec" DataMemberPath="QtyReceivedValidator" Width="50"/> 
                                                <telerikGridView:GridViewDataColumn Header="Remarks" DataMemberPath="RemarksValidator" Width="100"/> 

    All the binding work fine., only Selected Item it's not updated when clicking on the textbox.
  4. Rossen Hristov
    Rossen Hristov avatar
    2478 posts

    Posted 18 Jan 2010 Link to this post

    Hello Victor Manuel Gonzalez Tellez,

    This happens because the TextBox steals the focus and the underlying row does not even understand that it should be selected. You should listen for its focus and manually set the data item of the parent row to be the selected item. Something like this:

    private void TextBox_GotFocus(object sender, System.Windows.RoutedEventArgs e)
                var row = ((UIElement)sender).ParentOfType<GridViewRow>();
                this.MyDataGrid.SelectedItem = row.DataContext;

    If you do not want to directly attach to the event you can create an attached behavior like demonstrated in our Context Menu for Header Cells online example.

    I have attached a sample project with the latest binaries. I hope this helps.

    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Back to Top