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

Cannot bind header when gridview on second tab item

1 Answer 55 Views
GridView
This is a migrated thread and some comments may be shown as answers.
James
Top achievements
Rank 1
James asked on 14 Feb 2014, 02:51 PM
I have a gridview on a second tabitem within a tabcontrol, and the column header cannot be binded.

It gives error
System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='RadGridTest1.MainWindow', AncestorLevel='1''. BindingExpression:Path=DataContext.Header2; DataItem=null; target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String')

--
MainWindowViewModel.cs

public class MainWindowViewModel
{
public string Header2
{
get { return "Header 2"; }
}
}

--
MainWindow.xaml

<Window x:Class="RadGridTest1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
xmlns:local="clr-namespace:RadGridTest1"
Title="MainWindow" Height="350" Width="525">
<Window.DataContext>
<local:MainWindowViewModel />
</Window.DataContext>

<Grid>
<TabControl>
<TabItem Header="Tab 1">
</TabItem>

<TabItem Header="Tab 2">
<telerik:RadGridView AutoGenerateColumns="False">

<telerik:RadGridView.Columns>
<telerik:GridViewDataColumn>
<telerik:GridViewDataColumn.Header>
<TextBlock Text="{Binding Path=DataContext.Header2, RelativeSource={RelativeSource AncestorType={x:Type local:MainWindow}}}"/>
</telerik:GridViewDataColumn.Header>
</telerik:GridViewDataColumn>
</telerik:RadGridView.Columns>

</telerik:RadGridView>
</TabItem>
</TabControl>

</Grid>
</Window>

--
MainWindow.xaml.cs

public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
}

1 Answer, 1 is accepted

Sort by
0
Yoan
Telerik team
answered on 14 Feb 2014, 04:50 PM
Hi,

I would suggest you to define a StaticResource and then set the binding specifying a valid Source (Source={StaticResource MainWindowViewModel}) for it. Please check the following code snippet for a reference:
<Window.Resources>
        <my:MainWindowViewModel x:Key="MainWindowViewModel"/>
    </Window.Resources>
    <Grid DataContext="{StaticResource MainWindowViewModel}">
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
        <TabControl Name="SubscreenTabControl" Grid.Row="0">
            <TabItem Header="Tab 1">
                  
            </TabItem>
  
            <TabItem Header="Tab 2">
                <telerik:RadGridView AutoGenerateColumns="False">
  
                    <telerik:RadGridView.Columns>
                        <telerik:GridViewDataColumn>
                            <telerik:GridViewDataColumn.Header>
                                <TextBlock Text="{Binding Header2,Source={StaticResource MainWindowViewModel}}"/>
                            </telerik:GridViewDataColumn.Header>
                        </telerik:GridViewDataColumn>
                    </telerik:RadGridView.Columns>
  
                </telerik:RadGridView>
            </TabItem>
        </TabControl>

I hope this helps.


Regards,
Yoan
Telerik

Check out the new Telerik Platform - the only modular platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native apps. Register for the free online keynote and webinar to learn more about the Platform on Wednesday, February 12, 2014 at 11:00 a.m. ET (8:00 a.m. PT).

Tags
GridView
Asked by
James
Top achievements
Rank 1
Answers by
Yoan
Telerik team
Share this question
or