【发布时间】:2015-06-04 18:03:19
【问题描述】:
我在 XAML 中将 ItemsControl 定义为:
<ItemsControl ItemsSource="{Binding MyCollection}"
AlternationCount="{Binding RelativeSource={RelativeSource Self}, Path=Items.Count}">
<ItemsControl.Resources>
<DataTemplate x:Key="TemplateOne">
<Button Content="{Binding RelativeSource={RelativeSource Self}, Path=(ItemsControl.AlternationIndex)}" Style="{StaticResource StyleOne}"/>
</DataTemplate>
<DataTemplate x:Key="TemplateTwo">
<Button Content="{Binding RelativeSource={RelativeSource Self}, Path=(ItemsControl.AlternationIndex)}" Style="{StaticResource StyleTwo}"/>
</DataTemplate>
</ItemsControl.Resources>
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemContainerStyle>
<Style TargetType="ContentPresenter">
<Style.Triggers>
<Trigger Property="ItemsControl.AlternationIndex" Value="0">
<Setter Property="ContentTemplate" Value="{StaticResource TemplateOne}"></Setter>
</Trigger>
<Trigger Property="ItemsControl.AlternationIndex" Value="1">
<Setter Property="ContentTemplate" Value="{StaticResource TemplateTwo}"></Setter>
</Trigger>
</Style.Triggers>
</Style>
</ItemsControl.ItemContainerStyle>
</ItemsControl>
我可以根据 ItemsControl 的当前交替索引设置不同的模板。虽然这有效并为我提供了不同的数据模板,但我还希望 Button 的内容显示其交替索引,即 MyCollection 中项目的索引。有什么想法我可能会出错吗?
【问题讨论】:
标签: wpf itemscontrol