Bind to GridViewColumn.IsVisible in Child Grid

5 posts, 0 answers
  1. jen
    jen avatar
    63 posts
    Member since:
    Jun 2009

    Posted 15 Apr Link to this post

    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?

     

     

     

     

     

  2. Ivan Ivanov
    Admin
    Ivan Ivanov avatar
    1128 posts

    Posted 18 Apr Link to this post

    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.
  3. UI for WPF is Visual Studio 2017 Ready
  4. jen
    jen avatar
    63 posts
    Member since:
    Jun 2009

    Posted 18 Apr in reply to Ivan Ivanov Link to this post

    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?

     

  5. Ivan Ivanov
    Admin
    Ivan Ivanov avatar
    1128 posts

    Posted 21 Apr Link to this post

    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.
  6. jen
    jen avatar
    63 posts
    Member since:
    Jun 2009

    Posted 21 Apr in reply to Ivan Ivanov Link to this post

    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;
        }
    }

Back to Top
UI for WPF is Visual Studio 2017 Ready