包含动态对象的绑定ListCollectionView不显示网格WPF中的列

时间:2014-02-28 09:45:54

标签: .net wpf mvvm radgridview listcollectionview

我正在使用telerik RadGridView和一个源自DynamicObject的视图模型。

当此视图模型通过ObservableCollection<T>绑定到网格时,所有列都按预期显示,但是当可观察集合用作ListCollectionView的源时,然后将其绑定到网格,则NO列显示。

如何让ListCollectionView使用动态对象和网格控件?

下面显示了一个示例,当我绑定'AllItems'然后列'Value1'和&amp;显示“Value2”,但是当我绑定FilteredItems时,没有显示任何列。

public abstract class BaseViewModel<T> : DynamicObject
{

}

public class MyItemViewModel : BaseViewModel<MyItem>
{
    private MyItem _myItem;

    public MyItemViewModel(int i)
    {
        _myItem = new MyItem();
        _myItem.Value1 = "Ollie " + i;
        _myItem.Value2 = 42 + i;
    }

    public override IEnumerable<string> GetDynamicMemberNames()
    {
        return new[] {"Value1", "Value2"};
    }

    public override bool TryGetMember(GetMemberBinder binder, out object result)
    {
        if (binder.Name == "Value1")
        {
            result = _myItem.Value1;
            return true;
        }
        else if (binder.Name == "Value2")
        {
            result = _myItem.Value2;
            return true;
        }

        return base.TryGetMember(binder, out result);
    }

    public override bool TrySetMember(SetMemberBinder binder, object value)
    {
        if (binder.Name == "Value1")
        {
            _myItem.Value1 = (string)value;
            return true;
        }
        else if (binder.Name == "Value2")
        {
            _myItem.Value2 = (int)value;
            return true;
        }

        return base.TrySetMember(binder, value);
    }
}


public class TestHarness
{
    private readonly ObservableCollection<MyItemViewModel> _items = null; 
    private readonly ICollectionView _filteredItems = null;

    public TestHarness()
    {
        _items = new ObservableCollection<MyItemViewModel>();
        _filteredItems = new MyListCollectionView(_items);

        _items.Add(new MyItemViewModel(1));
        _items.Add(new MyItemViewModel(2));
        _items.Add(new MyItemViewModel(3));
    }

    public IEnumerable<MyItemViewModel> AllItems { get { return _items; } }

    public IEnumerable FilteredItems { get { return _filteredItems; } } 
}

0 个答案:

没有答案