Hi,
I'm trying to display a standard context menu on a RadGridView that lists the columns that are contained within the grid. The purpose of this is so that I can have a checkbox on the menu item that controls/changes the visibility of the column on the grid. I've seen the article (http://www.telerik.com/help/wpf/radgridview-how-to-show-hide-columns-outside-of-the-radgridview.html) that shows how to dynamically populate a listbox with checkboxes using XAML and bindings - this is close to what I want.
I've tried to add the context menu directly to the RadGridView using the following XAML but had no luck so far:
<telerik:RadGridView HorizontalAlignment="Left" Margin="15" Name="radGridView1" VerticalAlignment="Top" AutoGenerateColumns="False"
ItemsSource="{Binding ElementName=TheContext, Path=TestModels}">
<telerik:RadGridView.Columns>
<telerik:GridViewDataColumn Header="AA" DataMemberBinding="{Binding Path=AA}" />
<telerik:GridViewDataColumn Header="BB" DataMemberBinding="{Binding Path=BB}" />
</telerik:RadGridView.Columns>
<telerik:RadGridView.ContextMenu>
<ContextMenu>
<MenuItem Header="Columns" ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=telerik:RadGridView, AncestorLevel=1}, Path=Columns, Mode=TwoWay}">
<MenuItem.ItemTemplate>
<HierarchicalDataTemplate>
<MenuItem Header="Binding Header, Mode=TwoWay}" IsCheckable="True" IsChecked="{Binding IsVisible, Mode=TwoWay}"/>
</HierarchicalDataTemplate>
</MenuItem.ItemTemplate>
</MenuItem>
</ContextMenu>
</telerik:RadGridView.ContextMenu>
</telerik:RadGridView>
In the Output window I can see I get the following error message: System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='Telerik.Windows.Controls.RadGridView', AncestorLevel='1''. BindingExpression:Path=Columns; DataItem=null; target element is 'MenuItem' (Name=''); target property is 'ItemsSource' (type 'IEnumerable')
I thought perhaps I should try creating a context menu on something other than the RadGridView I've tried adding a context menu to a label that's outside of the RadGridView just to test things out but so far have called no joy. The XAML I was using for this was:
<Label>
<Label.Content>I'm a label but right click me...</Label.Content>
<Label.ContextMenu>
<ContextMenu Name="MainCollection">
<MenuItem Header="Columns">
<MenuItem ItemsSource="{Binding ElementName=radGridView1, Path=Columns, Mode=TwoWay}">
<MenuItem.ItemTemplate>
<HierarchicalDataTemplate>
<MenuItem Header="Binding Header, Mode=TwoWay}" IsCheckable="True" IsChecked="{Binding IsVisible, Mode=TwoWay}"/>
</HierarchicalDataTemplate>
</MenuItem.ItemTemplate>
</MenuItem>
</MenuItem>
</ContextMenu>
</Label.ContextMenu>
</Label>
From the output window I get the following error message "System.Windows.Data Error: 4 : Cannot find source for binding with reference 'ElementName=radGridView1'. BindingExpression:Path=Columns; DataItem=null; target element is 'MenuItem' (Name=''); target property is 'ItemsSource' (type 'IEnumerable')"
Any ideas welcome,
Chris.
I'm trying to display a standard context menu on a RadGridView that lists the columns that are contained within the grid. The purpose of this is so that I can have a checkbox on the menu item that controls/changes the visibility of the column on the grid. I've seen the article (http://www.telerik.com/help/wpf/radgridview-how-to-show-hide-columns-outside-of-the-radgridview.html) that shows how to dynamically populate a listbox with checkboxes using XAML and bindings - this is close to what I want.
I've tried to add the context menu directly to the RadGridView using the following XAML but had no luck so far:
<telerik:RadGridView HorizontalAlignment="Left" Margin="15" Name="radGridView1" VerticalAlignment="Top" AutoGenerateColumns="False"
ItemsSource="{Binding ElementName=TheContext, Path=TestModels}">
<telerik:RadGridView.Columns>
<telerik:GridViewDataColumn Header="AA" DataMemberBinding="{Binding Path=AA}" />
<telerik:GridViewDataColumn Header="BB" DataMemberBinding="{Binding Path=BB}" />
</telerik:RadGridView.Columns>
<telerik:RadGridView.ContextMenu>
<ContextMenu>
<MenuItem Header="Columns" ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=telerik:RadGridView, AncestorLevel=1}, Path=Columns, Mode=TwoWay}">
<MenuItem.ItemTemplate>
<HierarchicalDataTemplate>
<MenuItem Header="Binding Header, Mode=TwoWay}" IsCheckable="True" IsChecked="{Binding IsVisible, Mode=TwoWay}"/>
</HierarchicalDataTemplate>
</MenuItem.ItemTemplate>
</MenuItem>
</ContextMenu>
</telerik:RadGridView.ContextMenu>
</telerik:RadGridView>
In the Output window I can see I get the following error message: System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='Telerik.Windows.Controls.RadGridView', AncestorLevel='1''. BindingExpression:Path=Columns; DataItem=null; target element is 'MenuItem' (Name=''); target property is 'ItemsSource' (type 'IEnumerable')
I thought perhaps I should try creating a context menu on something other than the RadGridView I've tried adding a context menu to a label that's outside of the RadGridView just to test things out but so far have called no joy. The XAML I was using for this was:
<Label>
<Label.Content>I'm a label but right click me...</Label.Content>
<Label.ContextMenu>
<ContextMenu Name="MainCollection">
<MenuItem Header="Columns">
<MenuItem ItemsSource="{Binding ElementName=radGridView1, Path=Columns, Mode=TwoWay}">
<MenuItem.ItemTemplate>
<HierarchicalDataTemplate>
<MenuItem Header="Binding Header, Mode=TwoWay}" IsCheckable="True" IsChecked="{Binding IsVisible, Mode=TwoWay}"/>
</HierarchicalDataTemplate>
</MenuItem.ItemTemplate>
</MenuItem>
</MenuItem>
</ContextMenu>
</Label.ContextMenu>
</Label>
From the output window I get the following error message "System.Windows.Data Error: 4 : Cannot find source for binding with reference 'ElementName=radGridView1'. BindingExpression:Path=Columns; DataItem=null; target element is 'MenuItem' (Name=''); target property is 'ItemsSource' (type 'IEnumerable')"
Any ideas welcome,
Chris.