Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# BindingList投影包装器_C#_.net_Binding_Projection_Bindinglist - Fatal编程技术网

C# BindingList投影包装器

C# BindingList投影包装器,c#,.net,binding,projection,bindinglist,C#,.net,Binding,Projection,Bindinglist,有没有一种简单的方法来创建BindingList包装器(带投影),它会随着原始列表的更新而更新 例如,假设我有一个可变的数字列表,我想将它们表示为组合框中的十六进制字符串。使用此包装器,我可以执行以下操作: BindingList<int> numbers = data.GetNumbers(); comboBox.DataSource = Project(numbers, i => string.Format("{0:x}", i)); BindingList number

有没有一种简单的方法来创建
BindingList
包装器(带投影),它会随着原始列表的更新而更新

例如,假设我有一个可变的数字列表,我想将它们表示为组合框中的十六进制字符串。使用此包装器,我可以执行以下操作:

BindingList<int> numbers = data.GetNumbers();
comboBox.DataSource = Project(numbers, i => string.Format("{0:x}", i));
BindingList number=data.GetNumbers();
comboBox.DataSource=Project(数字,i=>string.Format(“{0:x}”,i));

我可以将列表包装成一个新的
绑定列表
,处理所有源事件,更新列表并再次触发这些事件,但我觉得已经有了一个更简单的方法。

我刚刚偶然发现了这个问题,我意识到我可能会发布我最终得到的代码

因为我想要一个快速的解决方案,我做了一个穷人的实现。它作为现有源列表的包装器工作,但它会创建一个完整的项目列表,并根据需要进行更新。起初,我希望可以在访问项目时动态地进行投影,但这需要从头开始实现整个
IBindingList
接口

is的作用:对源列表的任何更新也将更新目标列表,因此绑定控件将正确更新

它不做什么:当目标列表更改时,它不更新源列表。这就需要一个反向投影功能,而我根本不需要这个功能。因此,必须始终在源列表中添加、更改或删除项目

用法示例如下。假设我们有一个数字列表,但我们希望在数据网格中显示它们的平方值:

// simple list of numbers
List<int> numbers = new List<int>(new[] { 1, 2, 3, 4, 5 });

// wrap it in a binding list
BindingList<int> sourceList = new BindingList<int>(numbers);

// project each item to a squared item
BindingList<int> squaredList = new ProjectedBindingList<int, int>
    (sourceList, i => i*i);

// whenever the source list is changed, target list will change
sourceList.Add(6);
Debug.Assert(squaredList[5] == 36);
//简单的数字列表
列表编号=新列表(新[{1,2,3,4,5});
//将其包装在绑定列表中
BindingList sourceList=新的BindingList(编号);
//将每个项目投影为平方项目
BindingList squaredList=新项目BindingList
(sourceList,i=>i*i);
//无论源列表何时更改,目标列表都将更改
源列表。添加(6);
Assert(squaredList[5]==36);
以下是源代码:

public class ProjectedBindingList<Tsrc, Tdest> 
    : BindingList<Tdest>
{
    private readonly BindingList<Tsrc> _src;
    private readonly Func<Tsrc, Tdest> _projection;

    public ProjectedBindingList(
        BindingList<Tsrc> source, 
        Func<Tsrc, Tdest> projection)
    {
        _projection = projection;
        _src = source;
        RecreateList();
        _src.ListChanged += new ListChangedEventHandler(_src_ListChanged);
    }

    private void RecreateList()
    {
        RaiseListChangedEvents = false;
        Clear();

        foreach (Tsrc item in _src)
            this.Add(_projection(item));

        RaiseListChangedEvents = true;
    }

    void _src_ListChanged(object sender, ListChangedEventArgs e)
    {
        switch (e.ListChangedType)
        {
            case ListChangedType.ItemAdded:
                this.InsertItem(e.NewIndex, Proj(e.NewIndex));
                break;

            case ListChangedType.ItemChanged:
                this.Items[e.NewIndex] = Proj(e.NewIndex);
                break;

            case ListChangedType.ItemDeleted:
                this.RemoveAt(e.NewIndex);
                break;

            case ListChangedType.ItemMoved:
                Tdest movedItem = this[e.OldIndex];
                this.RemoveAt(e.OldIndex);
                this.InsertItem(e.NewIndex, movedItem);
                break;

            case ListChangedType.Reset:
                // regenerate list
                RecreateList();
                OnListChanged(e);
                break;

            default:
                OnListChanged(e);
                break;
        }
    }

    Tdest Proj(int index)
    {
        return _projection(_src[index]);
    }
}
公共类ProjectedBindingList
:BindingList
{
私有只读绑定列表_src;
私有只读函数投影;
公共项目绑定列表(
BindingList源,
(Func投影)
{
_投影=投影;
_src=源;
重新创建列表();
_src.ListChanged+=新的ListChangedEventHandler(\u src\u ListChanged);
}
私有void重新创建列表()
{
RaiseListChangedEvents=false;
清除();
foreach(Tsrc项目在src中)
增加(_投影(项目));
RaiseListChangedEvents=true;
}
void\u src\u ListChanged(对象发送方,ListChangedEventArgs e)
{
开关(如ListChangedType)
{
案例列表ChangedType.ItemAdded:
插入项(e.NewIndex,Proj(e.NewIndex));
打破
案例ListChangedType.ItemChanged:
此.Items[e.NewIndex]=Proj(e.NewIndex);
打破
案例列表ChangedType.ItemDeleted:
此.RemoveAt(e.NewIndex);
打破
案例列表ChangedType.ItemMoved:
Tdest movedItem=此[e.OldIndex];
此.RemoveAt(即旧索引);
插入项(例如NewIndex、movedItem);
打破
案例列表更改类型。重置:
//重新生成列表
重新创建列表();
唯一更改(e);
打破
违约:
唯一更改(e);
打破
}
}
Tdest项目(内部索引)
{
返回_投影(_src[index]);
}
}
我希望有人会觉得这很有用