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

Bind to GridViewColumn.IsVisible in Child Grid

4 Answers 87 Views
GridView
This is a migrated thread and some comments may be shown as answers.
jen
Top achievements
Rank 1
jen asked on 15 Apr 2016, 03:55 PM

Hello,

I have a RadGridView with child RadGridView . Both grids have a GridViewColumn whose visibility I would like to bind to a Property in my Model. This is working for the parent grid, but the child grid isn't in the right DataContext to access the property in my model.

The tables, stripped down for brevity:

 <telerik:RadGridView  AutoGenerateColumns="False" ItemsSource="{Binding ProductTypes.View}">
                        <telerik:RadGridView.ChildTableDefinitions>
                            <telerik:GridViewTableDefinition>
                                <telerik:GridViewTableDefinition.Relation>
                                    <telerik:PropertyRelation ParentPropertyName="kcc_IncentiveProductTypeChildren" />
                                </telerik:GridViewTableDefinition.Relation>
                            </telerik:GridViewTableDefinition>
                        </telerik:RadGridView.ChildTableDefinitions>

                        <telerik:RadGridView.Columns>
                           
                            <telerik:GridViewColumn  IsVisible="{Binding DataContext.CanView, ElementName=LayoutRoot}" >
                                <telerik:GridViewColumn.CellTemplate>
                                    <DataTemplate>
                                        <Button Content="Delete"/>
                                    </DataTemplate>
                                </telerik:GridViewColumn.CellTemplate>
                            </telerik:GridViewColumn>
                        </telerik:RadGridView.Columns>
                        <telerik:RadGridView.HierarchyChildTemplate>
                            <DataTemplate>
                                <telerik:RadGridView  ItemsSource="{Binding kcc_IncentiveProductTypeChildren}" AutoGenerateColumns="False"  >
                                  
                                    <telerik:RadGridView.Columns>
                                       
                                        <telerik:GridViewColumn  IsVisible="{Binding DataContext.CanView, ElementName=LayoutRoot}" >
                                            <telerik:GridViewColumn.CellTemplate>
                                                <DataTemplate>
                                                    <Button Content="Delete"/>
                                                </DataTemplate>
                                            </telerik:GridViewColumn.CellTemplate>
                                        </telerik:GridViewColumn>

                                    </telerik:RadGridView.Columns>
                                </telerik:RadGridView>
                            </DataTemplate>
                        </telerik:RadGridView.HierarchyChildTemplate>
                    </telerik:RadGridView>

 

I need a way to bind to the IsVisible property of the GridViewColumn in the child Grid.

I considered applying a style from my static resources to the GridViewColumn to set the IsVisible property, but GridViewColumn does not have a Style property! I am not sure, but I don't think the other styles (header/footer/cell) will work on this property of the GridViewColumn.

        <Style TargetType="telerik:GridViewColumn" x:Key="ViewPermissionColumn">
            <Setter Property="IsVisible"  Value="{Binding DataContext.CanView, ElementName=LayoutRoot}" />
        </Style>

 

As a last option, I assume I'll need to handle an event on the main grid in order to set this value manually in the code behind. What event would I need?

 

 

 

 

 

4 Answers, 1 is accepted

Sort by
0
Ivan Ivanov
Telerik team
answered on 18 Apr 2016, 04:19 PM
Hello,

Would it be possible for you to replace the ElementName source with x:Reference source? DataTemplates define their own namescope, which often causes bindings to fial due to unavailable ElementName source. Here is a forum thread that discusses a similar scenarios with DataGrid.

Regards,
Ivan Ivanov
Telerik
Do you need help with upgrading your AJAX, WPF or WinForms project? Check the Telerik API Analyzer and share your thoughts.
0
jen
Top achievements
Rank 1
answered on 18 Apr 2016, 04:55 PM

Oh no, I posted this question under WPF instead of under Sliverlight! (I'm working in multiple projects right now)

The x:Reference does not work in Silverlight, unfortunately.

Could you tell me the event I need instead? on-row-created or something like that?

 

0
Ivan Ivanov
Telerik team
answered on 21 Apr 2016, 03:59 PM
Hello,

I am afraid that Silverlight does not provide a direct workaround to this limitation. Would it be possible for you to expose the ViewModel instance that contains the visibility data, as a static resource? In this way you will be able to refer to the binding source explicitly.

Regards,
Ivan Ivanov
Telerik
Do you need help with upgrading your AJAX, WPF or WinForms project? Check the Telerik API Analyzer and share your thoughts.
0
jen
Top achievements
Rank 1
answered on 21 Apr 2016, 04:12 PM

I got the answer I needed from support. The event I was asking for was the DataLoading event.

 

private void RadGridView_DataLoading(object sender, GridViewDataLoadingEventArgs e)
{
    GridViewDataControl dataControl = (GridViewDataControl)sender;
    if (dataControl.ParentRow != null)
    {
        var column = dataControl.Columns[0];
        column.IsVisible = Model.IsColumnVisible;
    }
}

Tags
GridView
Asked by
jen
Top achievements
Rank 1
Answers by
Ivan Ivanov
Telerik team
jen
Top achievements
Rank 1
Share this question
or