如何在WPF列表框实例中获取列表框索引?

| 列表框是数据绑定的,通过xmldataprovider绑定到XML节点的集合。
已邀请:
我有一个类似的问题,在这里回答了 基本上,您将ListBox的ѭ0设置为很高,然后绑定到每个项目的ѭ1
<ListBox AlternationCount=\"100\">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <TextBlock Text=\"{Binding RelativeSource={RelativeSource Mode=TemplatedParent},
                                      Path=(ItemsControl.AlternationIndex)}\" />
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>
您可以从ItemContainerGenerator获取ListBoxItem的索引:
ItemContainerGenerator.IndexFromContainer(my_listBoxItem);
物业“ 4”将工作。这完全取决于您的绑定方式 您可能希望将``4''依赖项属性绑定到连接到其数据上下文的对象的某些属性,例如
<ListBox SelectedIndex=\"{Binding MySelectedIndex}\" ItemsSource=\"{Binding MyItems}\"/>
但是你显然可以做到这一点
<ListBox SelectedIndex=\"{Binding MySelectedIndex}\">
  <ListBoxItem>1</ListBoxItem>
  <ListBoxItem>2</ListBoxItem>
  <ListBoxItem>3</ListBoxItem>
  <ListBoxItem>4</ListBoxItem>
</ListBox>

要回复问题请先登录注册