每个选项卡都包含ListView,并且每次用户选择不同的选项卡时,我都需要更改此ListView的ItemsSource属性。如何在XAML中做到这一点?我尝试过
<TabItem Header="F2" ContentTemplate="{StaticResource TabItemTemplate}" Tag="t2">
<DataTrigger Binding="{Binding Path=IsSelected, RelativeSource={RelativeSource Mode=Self}}" Value="True">
<Setter TargetName="listView1" Property="ItemsSource" Value="{Binding t2C}"></Setter>
</DataTrigger>
</TabItem>
但是无法访问名为listView1的元素。
<Window.Resources>
<DataTemplate x:Key="TabItemTemplate">
<Grid DataContext="{Binding RelativeSource={RelativeSource AncestorType={x:Type Window}}}">
<ListView x:Name="listView1" ItemsSource="{Binding NewsPropCollection}">
<ListView.ItemContainerStyle>
<Style TargetType="{x:Type ListViewItem}">
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
</Style>
</ListView.ItemContainerStyle>
<ListView.ItemTemplate>
<DataTemplate>
<!-- Some code here -->
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
<Grid/>
</DataTemplate>
</Window.Resources>
<Grid>
<TabControl x:Name="tabControl" Grid.Row="1">
<TabItem Header="F1" ContentTemplate="{StaticResource TabItemTemplate}" Tag="t1" IsSelected="True"/>
<TabItem Header="F2" ContentTemplate="{StaticResource TabItemTemplate}" Tag="t2"></TabItem>
<TabItem Header="F3" ContentTemplate="{StaticResource TabItemTemplate}" Tag="t3"></TabItem>
<TabItem Header="F4" ContentTemplate="{StaticResource TabItemTemplate}" Tag="t4"></TabItem>
<TabItem Header="F5" ContentTemplate="{StaticResource TabItemTemplate}" Tag="t5"></TabItem>
</TabControl>
</Grid>