This question is locked. New answers and comments are not allowed.
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)
FolderViewModel (the child list viewmodel)
I extend the treelistview and named as ExtendedRadTreeListView
xaml: grid
in the xaml, i called the method :
this.RadTreeListView1.AddColumns();
Xmal.cs
I think it might be a bug when binding dynamically. Could you have a look at my code and help me? Thanks
Wei
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
> }
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