create dynamic raddropdownlist in dynamic table

2 posts, 0 answers
  1. Kevin
    Kevin avatar
    4 posts
    Member since:
    Sep 2012

    Posted 30 Oct 2013 Link to this post

    We have a lot of dynamic table to create columns(see attached file) basing on MVVM, We can not create codebehind in *.xaml.cs. This solution is ok to create genernal text for "GridViewDataColumn". But now we have a scenario to create dropdown list in cell. In this column, all itemsouce in dropdownlist are totally different. W

    due to following reson, we could not and it is impossible to using this.
    1. populate itemSource to columns in code behind
    2. could not change current architecture.

    we are using properity change event to dynamically create columns. additional, we only could assign binding in create columns. therefore, in many thread even in telerik sdk, we are using itemssourceBinding to do this. but it shows blank in cell when runtime.



    Please refer to attached sample code.  Is it possible to dynamically to create dropdown list basing on current design.

    Important note : our version is  2012.1.215.1040


    Add column code :
    if (field.Key == "1")
                      {
                          GridViewComboBoxColumn column = new GridViewComboBoxColumn();
                          column.Header = field.Value.Caption;
                          //data member binding
                          Binding dataMemberBinding = newBinding(String.Format("TemplateValues[{0}]", field.Key));
                          column.DataMemberBinding = dataMemberBinding;
     
                          //Items Source Binding
                          Binding itemBinding = newBinding(string.Format("TemplateDropDownValues[{0}]", field.Key));
                          column.ItemsSourceBinding = itemBinding;
     
                          column.DisplayMemberPath = "Name";
                          column.SelectedValueMemberPath = "ID";
     
                          control.Columns.Insert(currentIndex, column);
                      }
                      else if (field.Key == "2")
                      {
     
                          GridViewDataColumn column = new GridViewDataColumn();
                          column.Header = field.Value.Caption;
                          Binding bind = new Binding(String.Format("TemplateValues[{0}]", field.Key));
                          column.DataMemberBinding = bind;
                          control.Columns.Insert(currentIndex, column);
                      }

    ViewModel:
    public FolderViewModel(int schoolCount=1)
    {
     
        Dictionary<stringobject> values = new Dictionary<stringobject>();
        Dictionary<stringobject> lists = new Dictionary<stringobject>();
        //It is for dropdown list
        List<School> schools = new List<School>();
        School s1 = new School
        {
            ID = 1,
            schoolValue = 1,
            Name = "A-school"
        };
        if (schoolCount==2)
        {
            School s2 = new School
            {
                Name = "B-school",
                schoolValue = 2,
                ID = 2
            };
            schools.Add(s2);
        }
        
        schools.Add(s1);
       
        lists.Add("1", schools);
        TemplateDropDownValues = lists;
     
        //here for grid view
        values.Add("1", 0);
        TemplateValues = values;
        values.Add("2""This is in cell");
        
      }
     
     
    /// <summary>
    /// Grid view items
    /// </summary>
    public Dictionary<stringobject> TemplateValues
    {
        get;
        set;
    }
     
    /// <summary>
    /// drop list items
    /// </summary>
    public Dictionary<stringobject> TemplateDropDownValues
    {
        get;
        set;
    }

    ViewModelList
    public FolderListViewModel()
           {
               //add  2 rows
               ObservableCollection<FolderViewModel> folders = newObservableCollection<FolderViewModel>();
               FolderViewModel f1 = new FolderViewModel(1);
               FolderViewModel f2 = new FolderViewModel(2);
               folders.Add(f1);
               folders.Add(f2);
     
               //add 2 columns
               Dictionary<string, DataColumn> column = new Dictionary<string, DataColumn>();
               column.Add("1"new DataColumn("DropDownList Column"));
               column.Add("2"new DataColumn("General Column"));
               TemplateNames = column;
               Items = folders;
           }
           //binding items
           public ObservableCollection<FolderViewModel> Items { getset; }
     
        
           //columns
           public Dictionary<string, DataColumn> TemplateNames {getset;}

    ExtendTreeList control
    public class ExtendedRadTreeListView : RadTreeListView
       {
     
     
           private Dictionary<string, DataColumn> TemplateFields
           {
               get;
               set;
           }
     
           public Dictionary<string, DataColumn> DynamicColumns
           {
               get
               {
                   return (Dictionary<string, DataColumn>)GetValue(DynamicColumnsProperty);
               }
               set
               {
                   SetValue(DynamicColumnsProperty, value);
               }
           }
     
           public static readonly DependencyProperty DynamicColumnsProperty = DependencyProperty
                                                                               .Register("DynamicColumns"
                                                                                           typeof(Dictionary<string, DataColumn>)
                                                                                           typeof(ExtendedRadTreeListView), newPropertyMetadata(OnDynamicColumnsChanged));
     
           private static void OnDynamicColumnsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
           {
               ExtendedRadTreeListView grid = d as ExtendedRadTreeListView;
               if (e.NewValue != null && e.NewValue is Dictionary<string, DataColumn>)
               {
                   grid.TemplateFields = (e.NewValue as Dictionary<string, DataColumn>);
                   AddDynamicColumns.AddColumns(grid, grid.TemplateFields);
               }
           }
              
       }

    XAML
    <UserControl x:Class="SilverlightApplication27.MainPage"
     xmlns:controls="clr-namespace:SilverlightApplication27"
        mc:Ignorable="d"
        xmlns:Behaviors="clr-namespace:SilverlightApplication27;assembly=SilverlightApplication27"
                   xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
        d:DesignHeight="300" d:DesignWidth="400">
     
        <Grid x:Name="LayoutRoot" Loaded="LayoutRoot_Loaded" Background="White">
            <controls:ExtendedRadTreeListView x:Name="RadTreeListView1"
                                              AutoGenerateColumns="False" 
                                              CanUserFreezeColumns="False" 
                                              IsReadOnly="True"
                                              ItemsSource="{Binding Items, Mode=TwoWay}"
                                            DynamicColumns="{Binding TemplateNames, Mode=TwoWay}">
          <i:Interaction.Behaviors>
            <Behaviors:LoadBehavior/>
          </i:Interaction.Behaviors>
            </controls:ExtendedRadTreeListView>
        </Grid>
    </UserControl>

    Thanks a lot
  2. Dimitrina
    Admin
    Dimitrina avatar
    3769 posts

    Posted 01 Nov 2013 Link to this post

    Hi,

    As we already replied in the support thread you have opened, you can expose a property in your object that will hold all the properties you want and their corresponding values for each data item.
    I am attaching the solution illustrating the suggested approach. 

    Regards,
    Didie
    Telerik
    TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for SILVERLIGHT.
    Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
    Sign up for Free application insights >>
  3. DevCraft banner
Back to Top