This is a migrated thread and some comments may be shown as answers.

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

3 Answers 584 Views
GridView
This is a migrated thread and some comments may be shown as answers.
xp
Top achievements
Rank 1
xp asked on 31 Jul 2009, 06:34 PM
<telerikGrid:RadGridView Name="MyDataGrid"  ItemsSource="{Binding Path=MyDataCollection, Mode=TwoWay}" IsReadOnly="False" AutoGenerateColumns="False" >
                    <telerikGrid:RadGridView.Columns>
                           <telerikGrid:GridViewDataColumn HeaderText="Is Active" DataMemberPath="IsActive">
                            <telerikGrid:GridViewDataColumn.CellStyle>
                                <Style TargetType="telerikGridView:GridViewCell">
                                    <Setter Property="Template">
                                        <Setter.Value>
                                            <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}" />
                                                </Border>
                                            </ControlTemplate>
                                        </Setter.Value>
                                    </Setter>
                                </Style>
                            </telerikGrid:GridViewDataColumn.CellStyle>
                        </telerikGrid:GridViewDataColumn>

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.

thanks
                       

3 Answers, 1 is accepted

Sort by
0
Rossen Hristov
Telerik team
answered on 06 Aug 2009, 06:53 AM
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,
Ross
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.
0
Victor Manuel Gonzalez Tellez
Top achievements
Rank 1
answered on 15 Jan 2010, 04:33 PM
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}" 
                                         Grid.Column="1"
                                        <telerikGridView:RadGridView.Columns> 
                                            <telerikGridView:GridViewDataColumn Header="Item" DataMemberPath="ItemValidator" Width="82"
                                                <telerikGridView:GridViewDataColumn.CellStyle> 
                                                    <Style TargetType="GridView:GridViewCell"
                                                        <Setter Property="Template"
                                                            <Setter.Value> 
                                                                <ControlTemplate TargetType="GridView:GridViewCell"
                                                                     
                                                                        <TextBox IsEnabled="{Binding Value, Source={StaticResource bindingHelper}}" 
                                                                        Text="{Binding ItemValidator, Mode=TwoWay}" /> 
                                                               
                                                                </ControlTemplate> 
                                                            </Setter.Value> 
                                                        </Setter> 
                                                    </Style> 
                                                </telerikGridView:GridViewDataColumn.CellStyle> 
 
                                            </telerikGridView:GridViewDataColumn> 
                                            <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"/> 
                                        </telerikGridView:RadGridView.Columns> 
                                    </telerikGridView:RadGridView> 

All the binding work fine., only Selected Item it's not updated when clicking on the textbox.
0
Rossen Hristov
Telerik team
answered on 18 Jan 2010, 02:23 PM
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.

Regards,
Ross
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.
Tags
GridView
Asked by
xp
Top achievements
Rank 1
Answers by
Rossen Hristov
Telerik team
Victor Manuel Gonzalez Tellez
Top achievements
Rank 1
Share this question
or