Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.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
WPF:项目容器不是项目集合的一部分_Wpf - Fatal编程技术网

WPF:项目容器不是项目集合的一部分

WPF:项目容器不是项目集合的一部分,wpf,Wpf,例如,我需要计算出列表框中每个项目相对于当前所选项目的位置,因为我希望将当前所选项目之前的项目与当前所选项目之后的项目进行样式设置(通过触发器) 我尝试了不同的方法,其中之一是使用多值绑定和多值转换器 DataTrigger的XAML如下所示: <DataTrigger Value="True"> <DataTrigger.Binding> <MultiBinding Converter="{StaticResource rpc}">

例如,我需要计算出
列表框
中每个项目相对于当前所选项目的位置,因为我希望将当前所选项目之前的项目与当前所选项目之后的项目进行样式设置(通过
触发器

我尝试了不同的方法,其中之一是使用
多值绑定
多值转换器

DataTrigger
的XAML如下所示:

<DataTrigger Value="True">
    <DataTrigger.Binding>
        <MultiBinding Converter="{StaticResource rpc}">
            <Binding Path="." RelativeSource="{RelativeSource FindAncestor, AncestorType={x:Type ListBox}}" />
            <Binding Path="." RelativeSource="{RelativeSource FindAncestor, AncestorType={x:Type ListBoxItem}}" />
        </MultiBinding>
    </DataTrigger.Binding>

    <Setter Property="..." Value="..." />
</DataTrigger>

DataTrigger
用于
样式中,该样式在
列表框的
ItemTemplate
中使用

所提到的
转换器
“rpc”是一个
多值转换器

如果我在这个
多值转换器
Convert
函数中设置了一个断点,我会将
列表框
实例作为第一个值,将
列表框项
实例作为第二个值,因此
多绑定
工作

但是,如果我试图通过使用
ListBox.Items.IndexOf(ListBoxItem)
计算
ListBoxItem
ItemsCollection
ListBoxItem
的索引,我总是得到-1,就好像
ListBoxItem
不是
ItemsCollection
的成员一样

即使我通过检查项目的
内容
是否相等(
listBox.items.OfType(属于ListBoxItem.FirstOrDefault)(函数(x)x.Content.Equals(ListBoxItem.Content)))来找出
ListBoxItem
,我也不会得到
ListBoxItem

为什么会这样?
如果不是通过
IndexOf
,我怎么才能得到
ListBoxItem的索引呢?

您得到的是-1,因为
Items
集合不包含任何
ListBoxItem
元素,而是
ListBoxItem
的实际内容

试试这个:

var index = listBox.Items.IndexOf(listBoxItem.Content);

非常感谢。就这样。我认为
ItemCollection
将包含
ListBoxItems
,而不是实际的项目。