I found this nice example on https://github.com/telerik/xaml-sdk about creating a Column Chooser for the GridView. However in my scenario I'm creating a GridView via custom control and I want to create the Column Chooser via RadWindow. I am unfortuantely unable to bind the Listbox ItemSource to the Columns property of the grid, am I missing something?
Below is my code to better explain:
Xaml:
<Window x:Class="WpfApplication1.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"
Title="MainWindow">
<Window.Resources>
<DataTemplate x:Key="RadMenuContentTemplate">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="100" />
</Grid.RowDefinitions>
<TextBlock Text="Available columns:" />
<ListBox Grid.Row="1" ItemsSource="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=telerik:RadGridView}, Path=Columns}">
<ListBox.ItemTemplate>
<DataTemplate>
<CheckBox Content="{Binding Header}" IsChecked="{Binding IsVisible, Mode=TwoWay}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
</DataTemplate>
</Window.Resources>
<Grid>
<telerik:RadGridView x:Name="RadGridView1" ItemsSource="{Binding View}" Grid.Row="1" ShowGroupPanel="False" />
</Grid>
</Window>
Code-Behind:
public partial class MainWindow : Window
{
private readonly RadContextMenu defaultContextMenu = new RadContextMenu();
public MainWindow()
{
InitializeComponent();
var filterMenuItem = new RadMenuItem { Header = "Show/Hide Columns" };
filterMenuItem.Click += this.FilterMenuItemClick;
this.defaultContextMenu.Items.Add(filterMenuItem);
RadContextMenu.SetContextMenu(this.RadGridView1, this.defaultContextMenu);
DataContext = new MyDataContext();
}
private void FilterMenuItemClick(object sender, Telerik.Windows.RadRoutedEventArgs e)
{
var radWindow = new RadWindow
{
ContentTemplate = this.Resources["RadMenuContentTemplate"] as DataTemplate,
CanClose = true,
CanMove = false,
ResizeMode = ResizeMode.NoResize,
WindowStartupLocation = WindowStartupLocation.CenterScreen,
HideMinimizeButton = true,
HideMaximizeButton = true,
MinHeight = 100
};
radWindow.ShowDialog();
}
}
Below is my code to better explain:
Xaml:
<Window x:Class="WpfApplication1.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"
Title="MainWindow">
<Window.Resources>
<DataTemplate x:Key="RadMenuContentTemplate">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="100" />
</Grid.RowDefinitions>
<TextBlock Text="Available columns:" />
<ListBox Grid.Row="1" ItemsSource="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=telerik:RadGridView}, Path=Columns}">
<ListBox.ItemTemplate>
<DataTemplate>
<CheckBox Content="{Binding Header}" IsChecked="{Binding IsVisible, Mode=TwoWay}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
</DataTemplate>
</Window.Resources>
<Grid>
<telerik:RadGridView x:Name="RadGridView1" ItemsSource="{Binding View}" Grid.Row="1" ShowGroupPanel="False" />
</Grid>
</Window>
Code-Behind:
public partial class MainWindow : Window
{
private readonly RadContextMenu defaultContextMenu = new RadContextMenu();
public MainWindow()
{
InitializeComponent();
var filterMenuItem = new RadMenuItem { Header = "Show/Hide Columns" };
filterMenuItem.Click += this.FilterMenuItemClick;
this.defaultContextMenu.Items.Add(filterMenuItem);
RadContextMenu.SetContextMenu(this.RadGridView1, this.defaultContextMenu);
DataContext = new MyDataContext();
}
private void FilterMenuItemClick(object sender, Telerik.Windows.RadRoutedEventArgs e)
{
var radWindow = new RadWindow
{
ContentTemplate = this.Resources["RadMenuContentTemplate"] as DataTemplate,
CanClose = true,
CanMove = false,
ResizeMode = ResizeMode.NoResize,
WindowStartupLocation = WindowStartupLocation.CenterScreen,
HideMinimizeButton = true,
HideMaximizeButton = true,
MinHeight = 100
};
radWindow.ShowDialog();
}
}