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

[Solved] Binding to Parent element

2 Answers 311 Views
Grid for XAML
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Shashi
Top achievements
Rank 1
Shashi asked on 01 Mar 2013, 05:35 AM
I have a view model which looks somewhat like this

public class MyViewModel
{
    public string Name { get; set; }
    public ObservableCollection<MyModel> Items { get; set; }
}
public class MyModel
{
    public string ItemName { get; set; }
    public int ItemId { get; set; }
}
The RadDataGrid takes the Items as the ItemsSource, however, within the header (or even the cell contents) I want to show the Name property which I am unable to do so.
I have also set the view models as the DataContext of the page.

<Grid:RadDataGrid x:Name="dataGridInventCostPrice" Grid.Row="1"
                          ItemsSource="{Binding Path=ItemCostPrices}"
                          Background="{StaticResource TelerikRadGridBackground}"
                          AutoGenerateColumns="False">
    <Grid:DataGridTemplateColumn>
        <Grid:DataGridTemplateColumn.Header>
            <TextBlock>
                <Run Text="Item " />
                <Run Text="{Binding Name}" />
            </TextBlock>
         </Grid:DataGridTemplateColumn.Header>
         <Grid:DataGridTemplateColumn.CellContentTemplate>
             <DataTemplate>
                 <TextBlock>
                     <Run Text="{Binding ItemId}" />
                 </TextBlock>
             </DataTemplate>
         </Grid:DataGridTemplateColumn.CellContentTemplate>
     </Grid:DataGridTemplateColumn>
 </Grid:RadDataGrid.Columns>
</Grid:RadDataGrid>
With the standard ListView, the ItemDataTemplate would work with the above Binding

2 Answers, 1 is accepted

Sort by
0
Tsvyatko
Telerik team
answered on 01 Mar 2013, 09:05 AM
Hi Shashi,

Thank you for your feedback. The reason the binding does not work in the header is due to the fact that DataGridColumnHeader receives its DataGridColumn as DataContext. Unlike ListView, where only one header is present, RadDataGrid provides multiple ones (per column) and thus it needs additional information regarding header/sort direction/property name, etc. all of which is passed in the datacontext. Let us know if this clarifies the default behavior.

Nevertheless, you can easily achieve the desired behavior using binding to StaticResource or ElementName Binding. I have prepared sample project demonstrating the approach. Please have a look at it and let us know if you have any further questions.


Regards,
Tsvyatko
the Telerik team
Have a suggestion or face a problem - you can use the Ideas & Feedback portal to submit ideas, feedback and vote for them.
0
Shashi
Top achievements
Rank 1
answered on 04 Mar 2013, 12:08 AM
Thanks Tsvyatko,
That has worked to what I want it to do.
Tags
Grid for XAML
Asked by
Shashi
Top achievements
Rank 1
Answers by
Tsvyatko
Telerik team
Shashi
Top achievements
Rank 1
Share this question
or