Telerik GridView default XElement data source direct binding, will cause the built-in sort, filter, group and other functions can not be used.
The reason is that the functions of the Telerik GridView are implemented according to the data type, while the XElement binding is indistinguishable from the data source itself.
One solution is to convert the properties of the binding to specific properties, that is, the Telerik official website so-called various datasource XML binding method
Another solution for the actual project is to turn the data source into a dynamic data source, and then bind the Itemsource to the Dynamic Data source.
There might be other ways, too, I didn't test it out.
The code is as follows:
public void LoadXMLData (XElement data) { if (data! = null) {this . Items = new Observablecollection<dynamic> (from element in data. Elements () Select New DataRow (todictionary (Element), Element)); } }
Private idictionary<string, object> todictionary (XElement Element) { var dict = new Dictionary<string , object> (); foreach (var e in element. Elements ()) { dict. ADD (E.name.localname, E.value); } return dict; }
Add a class that transforms the dynamic:
public class Datarow:dynamicobject, inotifypropertychanged {readonly idictionary<string, object> data; Public XElement Itemxml {get; set;} Public DataRow (idictionary<string, object> source,xelement item) {data = source; item = Item; } public override ienumerable<string> Getdynamicmembernames () {return data. Keys; } public override bool Trygetmember (GetMemberBinder binder, out object result) {result = This[bi NDEr. Name]; return true; } public override bool Trysetmember (SetMemberBinder Binder, Object value) {This[binder. Name] = value; return true; } public Object This[string ColumnName] {get {if (data. ContainsKey (ColumnName)) {return data[columnname]; } return null; } set {if (!data. ContainsKey (ColumnName)) {data. ADD (columnName, value); OnPropertyChanged (ColumnName); } else {if (Data[columnname]! = value) { Data[columnname] = value; OnPropertyChanged (ColumnName); }}}} private void OnPropertyChanged (String propertyname) {if (propertychanged! = null) {propertychanged (this, new PropertyChangedEventArgs (PropertyName)); }} #region INotifyPropertyChanged members public event PropertyChangedEventHandler PropertyChanged; #endregion}
Telerik problems with XML data source bindings