0

我有一个ObservableCollection<T>可以容纳一种类型的对象。我需要它作为几个列表控件的来源,但我也想转换数据。为简单起见,假设我有一个ObservableCollection<int>并且想要得到一个ICollectionView返回字符串的 - 可能只是转换为字符串的整数。

有没有办法创建这样的视图?

不错的东西,例如:

var view = new MagicalCollectionView(myCollection, x => x.ToString())?
4

3 回答 3

1

集合视图是关于排序、过滤、分组的。它们与数据转换无关。

实际上,您的任务解释中没有直接需要这样的集合。您只需要正确设置视图DataTemplate,例如为源集合中的项目提供正确的:

public class MyEntity
{
    public int Id { get; set; }
    public string Name { get; set; }
}

public class MyViewModel
{
    public ObservableCollection<MyEntity> Entities { get; private set; }
    public ICollectionView EntitiesView
    {
        if (view == null)    
        {
            view = new ListCollectionView(Entities);
        }
        return view;
    }
    private ICollectionView view;
}

XAML:

<DataTemplate DataType="{x:Type local:MyEntity}">
    <!-- This "transforms" visual representation of your entity, but the entity itself (and its container) remains unchanged -->
    <TextBlock Text="{Binding Name}"/>
</DataTemplate>

更新。根据您的评论,我会将实体包装到视图模型中。此外,这是视图模型的目的:

public class MyEntityViewModel
{
    private readonly MyEntity model;

    public MyEntityViewModel(MyEntity model)
    {
        this.model = model;
    }

    public int MyInt
    {
        get { return model. // some logic to retrieve int ... }
    }

    public string MyString
    {
        get { return model. // some logic to retrieve string ... }
    }
}

然后,我将控件绑定到视图模型集合,而不是模型集合:

public class MyViewModel
{
    public MyViewModel(ICollection<MyEntity> entities)
    {
        this.Entities = new ObservableCollection<MyEntityViewModel>(entities.Select(e => new MyEntityViewModel(e)));

        // this will keep two collections synchronized:
        this.Entities.CollectionChanged += (sender, args) =>
        {
            switch (e.Action)
            {
                case NotifyCollectionChangedAction.Add:
                    entities.Add((MyEntity)e.NewItems[0]);
                    break;
                case NotifyCollectionChangedAction.Remove:
                    entities.Remove((MyEntity)e.OldItems[0]);
                    break;
                case NotifyCollectionChangedAction.Replace:
                    entities.Remove((MyEntity)e.OldItems[0]);
                    entities.Add((MyEntity)e.NewItems[0]);
                    break;
                case NotifyCollectionChangedAction.Reset:
                    entities.Clear();
                    break;
            }
        }
    }            

    public ObservableCollection<MyEntityViewModel> Entities { get; private set; }
}

这将使您的数据类远离额外的属性,这些属性仅供查看。

于 2013-07-09T10:44:21.037 回答
0

但是,我大多同意 Dennis,但在这种情况下,使用绑定转换器对底层绑定值进行最终转换可能会对您有所帮助。

我假设您有一组类型,并且希望在不同位置对数据进行不同的、实时的投影。

您可以在每种情况下绑定到同一个集合,并为每种情况“插入”不同的自定义转换器,可能在自定义 DataTemplate 中。

http://www.silverlightshow.net/items/Data-Conversion-in-Silverlight-2-Data-Binding.aspx

Silverlight 演示,但它是一样的。

于 2013-07-09T10:54:45.333 回答
0

您可以使用 LINQ 进行投影

myCollection.Select(x=> int.Parse(x))

将解析集合中的每个元素并返回一个整数

于 2013-07-09T10:39:45.330 回答