【问题标题】:wpf,How to show index of Collection in bind?wpf,如何在绑定中显示集合的索引?
【发布时间】:2010-12-08 08:59:34
【问题描述】:

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>

【问题讨论】:

    标签: wpf


    【解决方案1】:

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

    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">
    

    【讨论】:

    • +1,正在考虑类似的事情。两件事:1)您可能希望在 ListBox 中将 AlternationCount 设置为 100000 之类的值。否则他们都会有 0 作为数字。 2) 编号将从 0 而不是 1 开始,因此您可能需要使用转换器并将值增加 1
    猜你喜欢
    • 2013-05-29
    • 1970-01-01
    • 2012-07-07
    • 2011-05-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-04
    • 1970-01-01
    相关资源
    最近更新 更多