Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/275.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# 列表框不是';我没有展示我的第一件物品_C#_Wpf_Mvvm - Fatal编程技术网

C# 列表框不是';我没有展示我的第一件物品

C# 列表框不是';我没有展示我的第一件物品,c#,wpf,mvvm,C#,Wpf,Mvvm,我在WPF和将我的listbox/listview绑定到自定义列表时遇到了一个小问题 此列表实现了IEnumerator和IEnumerable接口。如果我将控件绑定到这些对象,我就永远看不到列表中的第一个对象 当我创建gridview时,它确实显示了我的第一个对象。因此,出于某种原因,listbox/listview正在做不同的事情来枚举我的自定义列表 对于这两种设置,我的绑定方式完全相同,在我的ViewModel上使用公共属性 绑定(我的PersonObject有一个公共属性ProjectL

我在WPF和将我的listbox/listview绑定到自定义列表时遇到了一个小问题

此列表实现了IEnumerator和IEnumerable接口。如果我将控件绑定到这些对象,我就永远看不到列表中的第一个对象

当我创建gridview时,它确实显示了我的第一个对象。因此,出于某种原因,listbox/listview正在做不同的事情来枚举我的自定义列表

对于这两种设置,我的绑定方式完全相同,在我的ViewModel上使用公共属性

绑定(我的PersonObject有一个公共属性ProjectList,它获取我所说的自定义列表)

XAML:

<ListBox ItemsSource="{Binding Path=Person.ProjectList,UpdateSourceTrigger=PropertyChanged}" AlternationCount="2" ItemContainerStyle="{StaticResource CustomListBoxItemStyle}">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <StackPanel>
                            <TextBlock>
                            <TextBlock.Text>
                                <MultiBinding StringFormat="{}{0} - {1}">
                                    <Binding Path="Name" />
                                    <Binding Path="Number" />
                                </MultiBinding>
                            </TextBlock.Text>
                            </TextBlock>
                            <TextBlock>
                            <TextBlock.Text>
                                <MultiBinding StringFormat="{}{0} - {1}">
                                    <Binding Path="StartDate" />
                                    <Binding Path="EndDate" />
                                </MultiBinding>
                            </TextBlock.Text>
                            </TextBlock>
                        </StackPanel>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>

Customlist类:

public class ProjectList : DeletableSupport, IEnumerator, IEnumerable
{
    private IList<Project> _pList;

    private int _position;

    public ProjectList()
    {
        this._pList = new ActivatableList<Project>();
        this._position = -1;
    }

    public void Add(Project p)
    {
        Activate(ActivationPurpose.Write);
        this._pList.Add(p);
    }

    public void Remove(Project p)
    {
        Activate(ActivationPurpose.Write);
        this._pList.Remove(p);
    }

    public int Count()
    {
        Activate(ActivationPurpose.Read);
        return this._pList.Count();
    }

    public bool Contains(Project p)
    {
        Activate(ActivationPurpose.Read);
        return this._pList.Contains(p);
    }

    public Project this[int i]
    {
        get
        {
            Activate(ActivationPurpose.Read);
            return this._pList[i];
        }
        set
        {
            Activate(ActivationPurpose.Write);
            this._pList[i] = value;
        }
    }

    public static ProjectList operator +(ProjectList pList, Project p)
    {
        pList.Add(p);
        return pList;
    }

    public static ProjectList operator -(ProjectList pList, Project p)
    {
        pList.Remove(p);
        return pList;
    }

    #region IEnumerable Members

    public IEnumerator GetEnumerator()
    {
        Activate(ActivationPurpose.Read);
        return (IEnumerator)this;
    }

    #endregion

    #region IEnumerator Members

    public object Current
    {
        get
        {
            try
            {
                Activate(ActivationPurpose.Read);
                return this._pList[_position];
            }
            catch (IndexOutOfRangeException)
            {
                throw new InvalidOperationException();
            }
        }
    }

    public bool MoveNext()
    {
        Activate(ActivationPurpose.Read);
        this._position++;
        if (_position < this._pList.Count)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    public void Reset()
    {
        Activate(ActivationPurpose.Write);
        _position = -1;
    }

    #endregion
}
公共类项目列表:DeletableSupport、IEnumerator、IEnumerable
{
私人保险公司;
私人国际职位;
公共项目列表()
{
此._pList=new ActivatableList();
这个。_位置=-1;
}
公共空间添加(项目p)
{
激活(ActivationPurpose.Write);
本条增补(p);
}
公共空间移除(项目p)
{
激活(ActivationPurpose.Write);
这个。请删除(p);
}
公共整数计数()
{
激活(ActivationPurpose.Read);
返回此项。_pList.Count();
}
公共bool包含(项目p)
{
激活(ActivationPurpose.Read);
返回此。列表包含(p);
}
公共项目本[int i]
{
得到
{
激活(ActivationPurpose.Read);
把这个还给我;
}
设置
{
激活(ActivationPurpose.Write);
这是._pList[i]=值;
}
}
公共静态项目列表操作符+(项目列表pList,项目p)
{
pList.Add(p);
返回层;
}
公共静态项目列表操作符-(项目列表pList,项目p)
{
pList.Remove(p);
返回层;
}
#区域可数成员
公共IEnumerator GetEnumerator()
{
激活(ActivationPurpose.Read);
返回(IEnumerator)这个;
}
#端区
#区域IEnumerator成员
公共对象流
{
得到
{
尝试
{
激活(ActivationPurpose.Read);
返回此项。_pList[_position];
}
捕获(IndexOutOfRangeException)
{
抛出新的InvalidOperationException();
}
}
}
公共图书馆
{
激活(ActivationPurpose.Read);
这个;
如果(_位置

Activate来自db4o,ActivatableList实现了IList

我在这里冒昧猜测一下,只是为了测试我的心理调试技能:)

ListBox/ListView正在使用IEnumerable.Any()(或某些等效项)测试列表是否为空。这将返回true,因此它将使用IEnumerable实现实际遍历列表

请注意,它没有在类上调用reset,这意味着将跳过Any()调用检索到的第一个元素

通常,对IEnumerable调用GetEnumerator将为类返回IEnumerator的新实例,但实际的列表实现包含枚举器的所有状态。您是否考虑过如果再次将此列表传递到您的列表框会发生什么情况?我想你什么也看不见


好的,那么这是如何解决的呢?好的,根据您拥有的代码,只要有人调用GetEnumerator(),您就可以调用Reset()但是您的实现不是线程安全的(现在可能不是问题,但谁知道将来会如何使用它?)。如果你真的不想使用ObservableCollection之类的东西来存储你的项目列表,我至少会看看从GetEnumerator方法返回一个单独的IEnumerator实例,枚举过程的所有状态都保存在那里。

事实上,对于我的另一个回复,在某个特定点上不会调用重置,在这里进行一些调试以使事情再次运行。。。你的心理调试技能没有失败是的:)
public class ProjectList : DeletableSupport, IEnumerator, IEnumerable
{
    private IList<Project> _pList;

    private int _position;

    public ProjectList()
    {
        this._pList = new ActivatableList<Project>();
        this._position = -1;
    }

    public void Add(Project p)
    {
        Activate(ActivationPurpose.Write);
        this._pList.Add(p);
    }

    public void Remove(Project p)
    {
        Activate(ActivationPurpose.Write);
        this._pList.Remove(p);
    }

    public int Count()
    {
        Activate(ActivationPurpose.Read);
        return this._pList.Count();
    }

    public bool Contains(Project p)
    {
        Activate(ActivationPurpose.Read);
        return this._pList.Contains(p);
    }

    public Project this[int i]
    {
        get
        {
            Activate(ActivationPurpose.Read);
            return this._pList[i];
        }
        set
        {
            Activate(ActivationPurpose.Write);
            this._pList[i] = value;
        }
    }

    public static ProjectList operator +(ProjectList pList, Project p)
    {
        pList.Add(p);
        return pList;
    }

    public static ProjectList operator -(ProjectList pList, Project p)
    {
        pList.Remove(p);
        return pList;
    }

    #region IEnumerable Members

    public IEnumerator GetEnumerator()
    {
        Activate(ActivationPurpose.Read);
        return (IEnumerator)this;
    }

    #endregion

    #region IEnumerator Members

    public object Current
    {
        get
        {
            try
            {
                Activate(ActivationPurpose.Read);
                return this._pList[_position];
            }
            catch (IndexOutOfRangeException)
            {
                throw new InvalidOperationException();
            }
        }
    }

    public bool MoveNext()
    {
        Activate(ActivationPurpose.Read);
        this._position++;
        if (_position < this._pList.Count)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    public void Reset()
    {
        Activate(ActivationPurpose.Write);
        _position = -1;
    }

    #endregion
}