Binding Issue

6 posts, 1 answers
  1. Dongzhi
    Dongzhi avatar
    25 posts
    Member since:
    Jan 2014

    Posted 15 Oct 2014 Link to this post

    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();
            }
        }

  2. Nasko
    Admin
    Nasko avatar
    587 posts

    Posted 16 Oct 2014 Link to this post

    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.

     
  3. UI for WPF is Visual Studio 2017 Ready
  4. Dongzhi
    Dongzhi avatar
    25 posts
    Member since:
    Jan 2014

    Posted 17 Oct 2014 in reply to Nasko Link to this post

    This helped thank you.
  5. Dongzhi
    Dongzhi avatar
    25 posts
    Member since:
    Jan 2014

    Posted 20 Oct 2014 Link to this post

    Since I'm creating a custom control, I'm trying to call this from a style xaml and now having the following issue attached.
  6. Answer
    Nasko
    Admin
    Nasko avatar
    587 posts

    Posted 21 Oct 2014 Link to this post

    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.

     
  7. Dongzhi
    Dongzhi avatar
    25 posts
    Member since:
    Jan 2014

    Posted 23 Oct 2014 in reply to Nasko Link to this post

    This helped, thank you.
Back to Top
UI for WPF is Visual Studio 2017 Ready