我的wpf窗口中有ListView
控件,其中包含自定义模板,并且ItemSource
绑定到字符串集合:
<ListView ItemsSource="{Binding ListOfStrings}">
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Expander Visibility="{Binding ToSomethingIDontKnowTo}"/>
<TextBlock Text="{Binding}"/>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
我需要仅为第一行显示Expander
。我知道可以使用Trigger
更改第一个元素样式,如下所示:
<Style TargetType="ListViewItem">
<Style.Triggers>
<Trigger Property="ItemsControl.AlternationIndex" Value="0">
<Setter Property="SomeProperty" Value="SomeValue" />
</Trigger>
</Style.Triggers>
</Style>
但我没有找到将此方法应用于ListViewItem
的子控件的方法。
答案 0 :(得分:1)
这是您可以触发Expander
可见性
<ListView AlternationCount="{Binding ItemsSource.Count, RelativeSource={RelativeSource Self}}" ItemsSource="{Binding ListOfStrings}">
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Expander Header="My">
<Expander.Style>
<Style TargetType="Expander">
<Setter Property="Visibility" Value="Collapsed"></Setter>
<Style.Triggers>
<DataTrigger Binding="{Binding (ItemsControl.AlternationIndex), RelativeSource={RelativeSource FindAncestor, AncestorType=ListViewItem}}" Value="0">
<Setter Property="Visibility" Value="Visible"></Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</Expander.Style>
</Expander>
<TextBlock Text="{Binding}"/>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>