wpf,如何在绑定中显示Collection的索引?

时间:2010-12-08 08:59:35

标签: wpf

ListBox已绑定到一个字符串集合,我想要Button控件来显示集合中元素的索引。

<Style TargetType="ListBoxItem">
        <Setter Property="Margin" Value="2"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate>
                    <Grid Background="{TemplateBinding Background}">
                        <Border BorderThickness="1">
                            <Button Height="20" Width="20" Content="{Binding ???}"  Margin="2">
                            </Button>
                        </Border>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        </Style>

1 个答案:

答案 0 :(得分:4)

检查这些链接。您可能会找到您要找的内容

WPF: Binding lists in XAML- how can an item know its position in the list?

Numbered listbox

如果您遵循Numbered listbox中“七”所描述的方法  您的代码应该类似于

<Button Height="20" Width="20" Content="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=TemplatedParent.(ItemsControl.AlternationIndex)}"  Margin="2">