dynamic binding on a treelistview loses column filter

6 posts, 0 answers
  1. Grant
    Grant  avatar
    2 posts
    Member since:
    Aug 2012

    Posted 28 Apr 2011 Link to this post

    Hi,

    I tried to bind columns dynamically using a dictionary. The data binds ok but it loses all the column filters, I did set column.IsFilterabe = true and column.IsSortable = true.

    I have attached a demo project. In the project I added a method call AddColumns() to bind new columns to the treelistview. But all binded new columns lost the filter and sorting functions. Please help.

    FolderListViewModel (the parent list viewmodel)
     public class FolderListViewModel<br>    {<br>        public FolderListViewModel()<br>        {<br>            ObservableCollection<FolderViewModel> folders = new ObservableCollection<FolderViewModel>();<br>            for (int i = 0; i < 10; i++)<br>            {<br>                folders.Add(new FolderViewModel("Parent" + i.ToString(), false));<br>            }<br>            Items = folders;<br>        }<br><br>        public ObservableCollection<FolderViewModel> Items<br>        {<br>            get;<br>            set;<br>        }<br><br>        public Dictionary<string, string> TemplateNames<br>        {<br>            get<br>            {<br>                Dictionary<string, string> names = new Dictionary<string, string>();<br>                names.Add("1", "Email");<br>                names.Add("2", "Gender");<br>                names.Add("3", "DOB");<br>                return names;<br>            }<br>          <br>        }<br>    }
    FolderViewModel (the child list viewmodel)
    public class FolderViewModel<br>    {<br>        private string name;<br>        private bool isEmpty;<br>        private ObservableCollection<FolderViewModel> items;<br><br>        public FolderViewModel(string name,bool IsEmpty)<br>        {<br>            this.Name = name;<br>            this.Items = new ObservableCollection<FolderViewModel>();<br>        }<br><br>        public string Name<br>        {<br>            get<br>            {<br>                return this.name;<br>            }<br>            private set<br>            {<br>                this.name = value;<br>            }<br>        }<br>        public bool IsEmpty<br>        {<br>            get<br>            {<br>                return this.isEmpty;<br>            }<br>            private set<br>            {<br>                this.isEmpty = value;<br>            }<br>        }<br><br>        public Dictionary<string, string> TemplateValues<br>        {<br>            get {<br>                Dictionary<string, string> values = new Dictionary<string, string>();<br>                values.Add("1", "hello@aaa");<br>                values.Add("2", "Male");<br>                values.Add("3", "19821011");<br>                return values;<br>            }<br>           <br>        }<br><br><br>        public ObservableCollection<FolderViewModel> Items<br>        {<br>            get<br>            {<br>                return this.items;<br>            }<br>            private set<br>            {<br>                this.items = value;<br>            }<br>        }<br><br>        public void LoadChildren()<br>        {<br>            List<FolderViewModel> folders = new List<FolderViewModel>();<br>            for (int i = 0; i < 10; i++) {<br>                folders.Add(new FolderViewModel("AA" + i.ToString(), false));<br>            }<br><br>            foreach (FolderViewModel folder in folders)<br>            {<br>                this.Items.Add(folder);<br>            }<br>        }<br><br>    }

    I extend the treelistview and named as ExtendedRadTreeListView
     
    public class ExtendedRadTreeListView : RadTreeListView<br>    {<br>        public Dictionary<string, string> TemplateFields<br>        {<br>            get;<br>            set;                   <br>        }<br><br>        public void AddColumns()<br>        {<br>            foreach (KeyValuePair<string, string> field in TemplateFields)<br>            {<br>                GridViewDataColumn column = new GridViewDataColumn();<br>                column.Header = field.Value;<br>                Binding bind = new Binding(String.Format("TemplateValues[{0}]",field.Key));        <br>                //string bindPath = "OwnerName";<br>                //bind.Converter = new TemplateFormFieldIdToValueConverter();<br>                //bind.ConverterParameter = field;<br>                bind.Mode = BindingMode.TwoWay;<br>                column.DataMemberBinding = bind;<br>                column.Width = new GridViewLength(0, GridViewLengthUnitType.Auto);<br>                column.IsFilterable = true;<br>                column.IsSortable = true;<br><br>                this.Columns.Add(column);<br>            }<br>            this.UpdateLayout();<br><br>        }<br>    }

    xaml: grid
    Grid x:Name="LayoutRoot" Background="White" Loaded="LayoutRoot_Loaded"><br>        <controls:ExtendedRadTreeListView x:Name="RadTreeListView1" RowLoaded="RadTreeListView1_RowLoaded"<br>                                 AutoGenerateColumns="False" RowIsExpandedChanging="RadTreeListView1_RowIsExpandedChanging" CanUserFreezeColumns="False"<br>                                 IsReadOnly="True"<br>                                          ><br>            <telerik:RadTreeListView.ChildTableDefinitions><br>                <telerik:TreeListViewTableDefinition ItemsSource="{Binding Items}" /><br>            </telerik:RadTreeListView.ChildTableDefinitions><br>            <telerik:RadTreeListView.Columns><br>                <telerik:GridViewDataColumn Header="Name" DataMemberBinding="{Binding Name}"/><br>            </telerik:RadTreeListView.Columns><br>        </controls:ExtendedRadTreeListView><br><br>    </Grid>

    in the xaml, i called the method : 
    this.RadTreeListView1.AddColumns();

    Xmal.cs
     
      public partial class MainPage : UserControl<br>    {<br>        public MainPage()<br>        {<br>            InitializeComponent();<br>        }<br><br>        private void LayoutRoot_Loaded(object sender, RoutedEventArgs e)<br>        {<br>            FolderListViewModel folders = new FolderListViewModel();<br>            this.RadTreeListView1.ItemsSource = folders.Items;<br>            this.RadTreeListView1.TemplateFields = folders.TemplateNames;<br>            this.RadTreeListView1.AddColumns();<br>        }<br><br>        private void RadTreeListView1_RowLoaded(object sender, RowLoadedEventArgs e)<br>        {<br>            FolderViewModel folder = e.DataElement as FolderViewModel;<br>            if (folder != null)<br>            {<br>                if (!folder.IsEmpty)<br>                {<br>                    ((TreeListViewRow)e.Row).IsExpandable = true;<br>                }<br>                else<br>                {<br>                    ((TreeListViewRow)e.Row).IsExpandable = false;<br>                }<br>            }<br>        }<br><br>        private void RadTreeListView1_RowIsExpandedChanging(object sender, RowCancelEventArgs e)<br>        {<br>            FolderViewModel folder = e.Row.Item as FolderViewModel;<br>            if (folder != null && folder.Items.Count() == 0)<br>            {<br>                folder.LoadChildren();<br>            }<br>        }<br><br>    }



    I think it might be a bug when binding dynamically. Could you have a look at my code and help me? Thanks

    Wei
  2. Vanya Pavlova
    Admin
    Vanya Pavlova avatar
    2019 posts

    Posted 28 Apr 2011 Link to this post

    Hello Grant ,

     
    Thank you for sharing these snippets with us! I have prepared an example for you based on these, however everything works fine by my side. May you please check the attached project and let me know how it differs from yours. Thus we would be able to provide you with an appropriate solution. 


    If you need any further assistance do not hesitate to contact us. 


    Kind regards,
    Vanya Pavlova
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  3. DevCraft banner
  4. Grant
    Grant  avatar
    2 posts
    Member since:
    Aug 2012

    Posted 28 Apr 2011 Link to this post

    Hi,

    Thanks for the reply. I have downloaded the project and tested it. I found out the AddColumns() method has been commented out.

    That's why there are no new columns dynamically binded to the grid.

    Please make sure you have the code below in the AddColumns() method.

    foreach

     

     

    (KeyValuePair<string, string> field in TemplateFields)

     

     

    {

     

     

     

    GridViewDataColumn column = new GridViewDataColumn(); column.Header = field.Value;

     

     

     

     

    Binding bind = new Binding(String.Format("TemplateValues[{0}]", field.Key));

     

     

    bind.Mode =

     

    BindingMode.TwoWay;

     

     

    column.DataMemberBinding = bind;

     

    column.Width =

     

    new GridViewLength(0, GridViewLengthUnitType.Auto);

     

     

    column.IsFilterable =

     

    true;

     

     

    column.IsSortable =

     

    true;

     

     

     

     

    this.Columns.Add(column);

     

     

     

     

    this.UpdateLayout();

     

     

    }



    "Name" column is binded through ItemSource. But the columns(such as Email, Gender, DOB) binded through AddColumns() are displaying without filter.


    Thanks
  5. Vanya Pavlova
    Admin
    Vanya Pavlova avatar
    2019 posts

    Posted 29 Apr 2011 Link to this post

    Hi Grant ,

     

    I have already replied to your support ticket. 


    All the best,
    Vanya Pavlova
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  6. Joke
    Joke avatar
    3 posts
    Member since:
    Jul 2012

    Posted 24 Jul 2012 Link to this post

    Hi Grant ,
        I now encounter the same problem with this and whether you had solved this problem.Can you tell me how to solve it?

    thanks
  7. Vlad
    Admin
    Vlad avatar
    11100 posts

    Posted 25 Jul 2012 Link to this post

    Hello,

     Can you please check if DataType property of the column is not null? If this is the case you can simply set this property to your column data type.

    Greetings,
    Vlad
    the Telerik team

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

Back to Top
DevCraft banner