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>
答案 0 :(得分:4)
检查这些链接。您可能会找到您要找的内容
WPF: Binding lists in XAML- how can an item know its position in the list?
如果您遵循Numbered listbox中“七”所描述的方法 您的代码应该类似于
<Button Height="20" Width="20" Content="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=TemplatedParent.(ItemsControl.AlternationIndex)}" Margin="2">