This question is locked. New answers and comments are not allowed.
I'm binding a column header's text to a property on my view-model (i.e. the grid's DataContext). I think I have it working now, but it seems like the solution should have been simpler. Can someone please tell me if there's a better solution?
Here's how I got it to work. Basically one column header contains a GridFinder, which provides access to the DataContext of the grid, and all the columns bind to it by element name.
(For brevity, some code is omitted, and only one column is shown.)
Here's the code for GridFinder, most of which was copied from this forum post by Dave Curylo.
Here are some other things I tried, which did not work:
I also tried adding a new dependency property, HeaderBinding, to a class that inherits from RadGridView. It updates Header whenever HeaderBinding's value changes. That didn't seem to be the way to go either, and didn't always work.
Here's how I got it to work. Basically one column header contains a GridFinder, which provides access to the DataContext of the grid, and all the columns bind to it by element name.
(For brevity, some code is omitted, and only one column is shown.)
<telerik:RadGridView x:Name="ScheduleGrid" ItemsSource="{Binding Schedules}" > <telerik:RadGridView.Columns> <telerik:GridViewDataColumn> <telerik:GridViewDataColumn.Header> <StackPanel> <ce:GridFinder x:Name="GridFinder" /> <TextBlock DataContext="{Binding ParentGridView.DataContext, ElementName=GridFinder}" Text="{Binding DayOfWeek1}" /> </StackPanel> </telerik:GridViewDataColumn.Header> </telerik:GridViewDataColumn> </telerik:RadGridView.Columns></telerik:RadGridView>Here's the code for GridFinder, most of which was copied from this forum post by Dave Curylo.
public class GridFinder : FrameworkElement{ public GridViewDataControl ParentGridView { get; private set; } public GridFinder() { this.Loaded += (s, re) => { this.ParentGridView = GetContainingGrid(this); }; } private GridViewDataControl GetContainingGrid(DependencyObject value) { if (value != null) { DependencyObject parent = VisualTreeHelper.GetParent(value); if (parent != null) { GridViewRow gridViewRow = parent as GridViewRow; if (gridViewRow != null) { return gridViewRow.GridViewDataControl; } else { GridViewDataControl grid = parent as GridViewDataControl; if (grid != null) { return grid; } else { return GetContainingGrid(parent); } } } else { return null; } } return null; }}Here are some other things I tried, which did not work:
<telerik:GridViewDataColumn> <telerik:GridViewDataColumn.Header> <TextBlock Text="{Binding DataContext.DayOfWeek1, ElementName=ScheduleGrid}" /> </telerik:GridViewDataColumn.Header></telerik:GridViewDataColumn><telerik:GridViewDataColumn Header="{Binding DataContext.DayOfWeek1, ElementName=ScheduleGrid}" /><telerik:GridViewDataColumn Header="{Binding DayOfWeek1}" />I also tried adding a new dependency property, HeaderBinding, to a class that inherits from RadGridView. It updates Header whenever HeaderBinding's value changes. That didn't seem to be the way to go either, and didn't always work.