WPF listBox 显示每个Item的index值

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/SANYUNI/article/details/45575145

在显示出来的Item中显示出当前Item的index值

Xaml文件如下:

    <Window.Resources>
        <Style x:Key="wrapalListBox" TargetType="ListBox">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate>
                        <WrapPanel  Orientation="Horizontal" IsItemsHost="True" ScrollViewer.CanContentScroll="True"/>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>


        </Style>


        <DataTemplate x:Key="listItemTempalte">
            <Button  Width="40" Height="30" Content="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=TemplatedParent.(ItemsControl.AlternationIndex)}"/>
        </DataTemplate>
    </Window.Resources>
    <Grid>
        <ListBox Style="{ StaticResource wrapalListBox}" ItemTemplate="{StaticResource listItemTempalte}" ItemsSource="{Binding Items}" AlternationCount="{Binding Path=Items.Count}" >
            
        </ListBox>
    </Grid>


后台代码中给一个List类型的Items属性  呈现的效果如图



参考http://stackoverflow.com/questions/745568/numbered-listbox/748383

猜你喜欢

转载自blog.csdn.net/SANYUNI/article/details/45575145
今日推荐