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

Binding Issue

5 Answers 133 Views
Window
This is a migrated thread and some comments may be shown as answers.
Dongzhi
Top achievements
Rank 1
Dongzhi asked on 15 Oct 2014, 08:07 PM
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();
        }
    }

5 Answers, 1 is accepted

Sort by
0
Nasko
Telerik team
answered on 16 Oct 2014, 03:54 PM
Hi Dongzhi,

Thank you for the provided code snippet.

In order to bind the ListBox's ItemSource to the Columns property of the GridView first set the Content property of RadWindow to it:

var radWindow = new RadWindow
{
    Content = this.RadGridView1.Columns,
    ContentTemplate = this.Resources["RadMenuContentTemplate"] as DataTemplate,
    ResizeMode = ResizeMode.NoResize,
    WindowStartupLocation = WindowStartupLocation.CenterScreen,
    HideMinimizeButton = true,
    HideMaximizeButton = true,
    MinHeight = 250,
    MinWidth = 250
};

After this is done change the ItemSource's markup extension to just Binding - the ListBox will receive the GridView Columns as DataContext:

<ListBox Grid.Row="1" ItemsSource="{Binding}">

I hope that this will help you.

Regards,
Nasko
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Dongzhi
Top achievements
Rank 1
answered on 17 Oct 2014, 04:04 PM
This helped thank you.
0
Dongzhi
Top achievements
Rank 1
answered on 21 Oct 2014, 12:36 AM
Since I'm creating a custom control, I'm trying to call this from a style xaml and now having the following issue attached.
0
Accepted
Nasko
Telerik team
answered on 21 Oct 2014, 01:52 PM
Hello Dongzhi,

In order to get it working correctly when using RadWindow as UserControl, you would need to move the content of the ContentTemplate inside of the RadWindow UserControl. After that create an instance of it and set its DataContext to the GridView's columns. 

We have implemented this suggestion in a sample project and you could run it.

I hope this will help you.

Regards,
Nasko
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Dongzhi
Top achievements
Rank 1
answered on 23 Oct 2014, 03:52 PM
This helped, thank you.
Tags
Window
Asked by
Dongzhi
Top achievements
Rank 1
Answers by
Nasko
Telerik team
Dongzhi
Top achievements
Rank 1
Share this question
or