标签:
Telerik GridView 默认的 XElement 数据源的直接绑定,会导致内置的sort, filter ,group等功能无法使用.
原因在于Telerik GridView的那些功能是根据数据类型实现的,而XElement绑定在数据源本身是无法区分类型的。
一个解决方案是绑定的属性转换为具体的 一个个属性,也就是Telerik 官网所谓的various datasource当中XML的绑定方式
另外一个比较适合实际项目的解决方案是 讲数据源转变为Dynamic数据源,然后将ItemSource绑定到这个Dynamic数据源当中。
别的方式或许也有,我没试验出来.
代码如下:
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; }
添加一个转换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[binder.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 }
标签:
原文地址:http://www.cnblogs.com/Juniph/p/4253327.html