Menu code binding

5 posts, 0 answers
  1. Riccardo
    Riccardo avatar
    36 posts
    Member since:
    Apr 2009

    Posted 10 Apr 2009 Link to this post

    I'm study Radmenu Examples but every example use binding in xaml file.
    I'm trying to do it via (pure) code, on CS. I've a class List<object> with an hierical structure but I can't to bind it to the rad menu.

    The only way I found was to create a loop in my List and so generate every RadMenuItem, but this isn't binding.
    Thanks.
    ---
    Merlinox
    www.merlinox.com
  2. Boyan
    Admin
    Boyan avatar
    758 posts

    Posted 14 Apr 2009 Link to this post

    Hello Riccardo,
     
    Sorry for the late reply. You are only seeing examples with databinding in xaml because we think that it is the best way for binding and therefor we are sticking to it.

    I prepared a simple example for you that shows how to bind a hierarchical  DataCollection with minimum amount of  xaml and it is in C# code. If you don't want to have even a tiny bit of xaml I am afraid that the only way is to create a loop in your List as you have already considered. I advice you to stick with xaml Binding if  you don't necessarily need it to be pure C#.

    using System; 
    using System.Collections.Generic; 
    using System.Linq; 
    using System.Net; 
    using System.Windows; 
    using System.Windows.Controls; 
    using System.Windows.Documents; 
    using System.Windows.Input; 
    using System.Windows.Media; 
    using System.Windows.Media.Animation; 
    using System.Windows.Shapes; 
    using System.ComponentModel; 
    using System.Collections.ObjectModel; 
    using System.Windows.Markup; 
     
    namespace RadMenu 
        public partial class MenuBinding : UserControl 
        { 
            private DataItemCollection Data; 
            private Telerik.Windows.Controls.RadMenu menu1; 
            public MenuBinding() 
            { 
                InitializeComponent(); 
     
                this.Data = new DataItemCollection() 
                { 
                    new DataItem() { Text = "Item1"  }, 
                    new DataItem() { Text = "Item2"
                    Children = { 
                        new DataItem() { Text = "Sub2.1" }, 
                        new DataItem() { Text = "Sub2.2" } 
                    }}, 
                    new DataItem() { Text = "Item3"
                    Children = { 
                        new DataItem() { Text = "Sub3.1" }, 
                        new DataItem() { Text = "Sub3.2" } 
                    }} 
                }; 
     
                string templateXaml =  
                @"<telerik:HierarchicalDataTemplate xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""  
                    xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:telerik=""clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls"
                    x:Key=""template"" ItemsSource=""{Binding Children}""> 
                    <TextBlock Text=""{Binding Text}"" /> 
                </telerik:HierarchicalDataTemplate>"; 
     
                // var template = this.LayoutRoot.Resources["template"] as DataTemplate; 
                var template = XamlReader.Load(templateXaml) as DataTemplate; 
     
                this.menu1 = new Telerik.Windows.Controls.RadMenu(); 
                this.LayoutRoot.Children.Add(this.menu1); 
     
                this.menu1.ItemTemplate = template; 
                this.menu1.ItemsSource = this.Data; 
            } 
        } 
     
        public class DataItemCollection : ObservableCollection<DataItem> 
        { 
        } 
     
        public class DataItem : INotifyPropertyChanged 
        { 
            private DataItemCollection children; 
            public DataItemCollection Children 
            { 
                get 
                { 
                    if (this.children == null
                    { 
                        this.children = new DataItemCollection(); 
                    } 
                    return this.children; 
                } 
            } 
     
            private string text; 
            public string Text 
            { 
                get 
                { 
                    return this.text; 
                } 
                set 
                { 
                    if (this.text != value) 
                    { 
                        this.text = value; 
                        this.OnPropertyChanged("Text"); 
                    } 
                } 
     
            } 
            public event PropertyChangedEventHandler PropertyChanged; 
     
            private void OnPropertyChanged(string propertyName) 
            { 
                if (this.PropertyChanged != null
                { 
                    this.PropertyChanged(thisnew PropertyChangedEventArgs(propertyName)); 
                } 
            } 
        } 
     


    Best wishes,
    Boyan
    the Telerik team

    Check out Telerik Trainer , the state of the art learning tool for Telerik products.
  3. DevCraft banner
  4. Riccardo
    Riccardo avatar
    36 posts
    Member since:
    Apr 2009

    Posted 14 Apr 2009 Link to this post

    Thanks Boyan.
    So the only solution is to create an HierarchicalDataTemplate with inline binding, like you do injected in c# code?

  5. Valeri Hristov
    Admin
    Valeri Hristov avatar
    2252 posts

    Posted 15 Apr 2009 Link to this post

    Yes, in Silverlight you cannot instantiate DataTemplate with code, hence the sample cannot be implemented with pure code, e.g. without XamlReader.Load(). If you want to create the RadMenu structure only with code, you will have to cycle through your data objects and create the items "manually", setting properties and bindings depending your requirements. In Boyan's example RadMenu will create the items automatically, depending the HierarchicalDataTemplate, which is basically used to define the Parent-Child relationship (the ItemsSource property).

    Sincerely yours,
    Valeri Hristov
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
  6. Riccardo
    Riccardo avatar
    36 posts
    Member since:
    Apr 2009

    Posted 15 Apr 2009 Link to this post

    Thanks Valeri. I still did it.
    But Boyan did it creating a support RadMenu objcet and then putting it into original RadMenu control.
    I did it looping (hierarchment) my hierarchical object list and creating RadMenuItem.
    But is not binding.

    Thanks.
Back to Top
DevCraft banner