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

Populate Column Header from ResourceFile in Child DataGrid

4 Answers 95 Views
GridView
This is a migrated thread and some comments may be shown as answers.
mark
Top achievements
Rank 1
mark asked on 16 May 2011, 07:14 PM

Hello Telerik,

We use a ResourceFIle for all displayed text in our application in order to support Globalization.  This works great for the GridViewDataColumn.Header unless it is in a child GridView. 

In the example below, we were forced to use a custom header in in the RowDetailsTemplate in order for the Header to Display correctly.  If we set the Header property in _gridDetails like in _gridPing it does not work. There were no binding errors, but the Header text was the displaying the binding property name, not the ResourceFile property value.

Is this a known issue?  I would like to use the simpler syntax if possible.


Thank you,
-Mark

<datagrid:NstrumentDataGrid x:Name="_gridPing"  Grid.Row="1" ItemsSource="{Binding Responses}"  >
    <datagrid:NstrumentDataGrid.Columns>
        <telerik:GridViewToggleRowDetailsColumn />
        <telerik:GridViewDataColumn Header="{Binding RelativeSource={RelativeSource AncestorType=UserControl}, Path=DataContext.Resources.Tools.Address}" Width="150" DataMemberBinding="{Binding Path=Address}" />
        <telerik:GridViewDataColumn Header="{Binding RelativeSource={RelativeSource AncestorType=UserControl}, Path=DataContext.Resources.Tools.Bytes}" Width="75" DataMemberBinding="{Binding Path=BufferLength}"/>
        <telerik:GridViewDataColumn Header="{Binding RelativeSource={RelativeSource AncestorType=UserControl}, Path=DataContext.Resources.Tools.Sent}" Width="75" DataMemberBinding="{Binding Path=Sent}"/>
        <telerik:GridViewDataColumn Header="{Binding RelativeSource={RelativeSource AncestorType=UserControl}, Path=DataContext.Resources.Tools.Received}" Width="90" DataMemberBinding="{Binding Path=Received}"/>
        <telerik:GridViewDataColumn Header="{Binding RelativeSource={RelativeSource AncestorType=UserControl}, Path=DataContext.Resources.Tools.PercentLost}" Width="75" DataMemberBinding="{Binding Path=PercentLost}" />
        <telerik:GridViewDataColumn Header="{Binding RelativeSource={RelativeSource AncestorType=UserControl}, Path=DataContext.Resources.Tools.Min}" Width="75" DataMemberBinding="{Binding Path=Min}" />
        <telerik:GridViewDataColumn Header="{Binding RelativeSource={RelativeSource AncestorType=UserControl}, Path=DataContext.Resources.Tools.Avg}" Width="75" DataMemberBinding="{Binding Path=Avg}" />
        <telerik:GridViewDataColumn Header="{Binding RelativeSource={RelativeSource AncestorType=UserControl}, Path=DataContext.Resources.Tools.Max}" Width="75" DataMemberBinding="{Binding Path=Max}" />
    </datagrid:NstrumentDataGrid.Columns>
    <datagrid:NstrumentDataGrid.RowDetailsTemplate>
        <DataTemplate >
            <datagrid:NstrumentDataGrid x:Name="_gridDetail" ItemsSource="{Binding Responses}" Margin="25,0,0,0" BorderThickness="0" >
                <datagrid:NstrumentDataGrid.Columns>
                    <telerik:GridViewDataColumn Width="125" DataMemberBinding="{Binding Path=Status}" >
                        <telerik:GridViewDataColumn.Header>
                            <TextBlock Text="{Binding RelativeSource={RelativeSource AncestorType=UserControl}, Path=DataContext.Resources.Tools.Status}"/>
                        </telerik:GridViewDataColumn.Header>
                    </telerik:GridViewDataColumn>
                    <telerik:GridViewDataColumn Width="125" DataMemberBinding="{Binding Path=RoundTripTime}" >
                        <telerik:GridViewDataColumn.Header>
                            <TextBlock Text="{Binding RelativeSource={RelativeSource AncestorType=UserControl}, Path=DataContext.Resources.Tools.RTT}"/>
                        </telerik:GridViewDataColumn.Header>
                    </telerik:GridViewDataColumn>
                    <telerik:GridViewDataColumn Width="125" DataMemberBinding="{Binding Path=BufferLength}" >
                        <telerik:GridViewDataColumn.Header>
                            <TextBlock Text="{Binding RelativeSource={RelativeSource AncestorType=UserControl}, Path=DataContext.Resources.Tools.BufferLength}"/>
                        </telerik:GridViewDataColumn.Header>
                    </telerik:GridViewDataColumn>
                </datagrid:NstrumentDataGrid.Columns>
            </datagrid:NstrumentDataGrid>
        </DataTemplate>
    </datagrid:NstrumentDataGrid.RowDetailsTemplate>
    <telerik:RadContextMenu.ContextMenu >
        <telerik:RadContextMenu x:Name="_contextMenu"  Opened="RadContextMenu_Opened"/>
    </telerik:RadContextMenu.ContextMenu>
</datagrid:NstrumentDataGrid>

4 Answers, 1 is accepted

Sort by
0
Rossen Hristov
Telerik team
answered on 17 May 2011, 09:59 AM
Hello mark,

The DataContext of the elements inside the Row Details is the data item contained in the respective parent row. In other words, the row and the row details share the same DataContext, so you are free to bind the elements in your row details template to any of the properties of the respective data item (the business object). The topic about row details can be found here.

So you can't bind to the DataContext like that. I think that you will have to use some kind of StaticResource or some other binding approach, but this is a topic beyond the scope of Telerik Support.

Kind regards,
Ross
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
mark
Top achievements
Rank 1
answered on 17 May 2011, 07:46 PM

I understand that the DataContext is the item in the collection that I am binding to.  That is why I use RelativeSource binding to populate the Header on the parent GridView.  Thats the whole point of RelativeSource binding...

If the row and the row details have the same DataContext, then why can't the same RelativeSource binding statement would work on both?  It still seems like there is a problem...

0
Accepted
Rossen Hristov
Telerik team
answered on 18 May 2011, 10:01 AM
Hello mark,

The Binding in the child grid will not work because columns are not visual elements and the ones in the child grid cannot "climb" up the tree to find their ancestor. The ones from the parent grid can do it because they are not inside a DataTemplate, etc. It is a very long story and I will not go into details.

I have prepared a very simple project that demonstrates how to resolve this issue. I placed a TextBlock in the header and bound this TextBlock with the same Binding. This should make it work:

<Window x:Class="RadGridView_WPF_AR_4.MainWindow"
        xmlns:my="clr-namespace:RadGridView_WPF_AR_4"
        Title="MainWindow" Height="700" Width="700">
    <Window.Resources>
        <my:MyViewModel x:Key="MyViewModel"/>
    </Window.Resources>
 
    <my:MyGrid DataContext="{StaticResource MyViewModel}">
        <my:MyGrid.RowDefinitions>
            <RowDefinition Height="*"/>
            <RowDefinition Height="Auto" />
        </my:MyGrid.RowDefinitions>
        <my:MyGrid.Resources>
            <DataTemplate x:Key="RowDetailsTemplate">
                <telerik:RadGridView Name="playersGrid"
                                     ItemsSource="{Binding Players}"
                                     AutoGenerateColumns="False">
                    <telerik:RadGridView.Columns>
                        <telerik:GridViewDataColumn DataMemberBinding="{Binding Name}">
                            <telerik:GridViewDataColumn.Header>
                                <TextBlock Text="{Binding RelativeSource={RelativeSource AncestorType=my:MyGrid}, Path=DataContext.NameHeader}"/>
                            </telerik:GridViewDataColumn.Header>
                        </telerik:GridViewDataColumn>
                    </telerik:RadGridView.Columns>
                </telerik:RadGridView>
            </DataTemplate>
        </my:MyGrid.Resources>
        <telerik:RadGridView Grid.Row="0"
                             Name="clubsGrid"
                             ItemsSource="{Binding Clubs}"
                             AutoGenerateColumns="False"
                             RowDetailsTemplate="{StaticResource RowDetailsTemplate}">
            <telerik:RadGridView.Columns>
                <telerik:GridViewToggleRowDetailsColumn/>
                <telerik:GridViewDataColumn DataMemberBinding="{Binding Name}"/>
            </telerik:RadGridView.Columns>
        </telerik:RadGridView>
    </my:MyGrid>
</Window>

You can find the sample project attached.

Can you try this approach and see whether it will solve the issue. Instead of binding the Header directly -- place a TextBlock and bind it instead. 

Let me know if this does not help. 

All the best, Ross
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
mark
Top achievements
Rank 1
answered on 18 May 2011, 03:36 PM
Thank you for your reply. 

Your solution was exactly what I was doing in my original post.  I just couldn't understand why the RelativeSource binding would work in my custom header textblock inside the RowDetailsTemplate, but not on the Header property itself. 

I will continue to use the more verbose syntax.

Thanks,
Mark
Tags
GridView
Asked by
mark
Top achievements
Rank 1
Answers by
Rossen Hristov
Telerik team
mark
Top achievements
Rank 1
Share this question
or