【发布时间】:2014-10-28 15:54:05
【问题描述】:
我有一个ListView,里面还有一个ListView。每当我在子 ListView 中选择一个项目时,我希望在父 ListView 中选择该项目的父项。示例:
<Window>
<Window.Resources>
<!-- Parent ListView ItemsTemplate... Incomplete -->
<DataTemplate x:Key="parentItemTemplate">
<!-- Child ListView -->
<ListView SelectedItem="{Binding ChildSelectedItem}" ItemsSource="{Binding WhateverInParent}">
<ListView.Resources>
<Style TargetType="ListViewItem">
<Trigger Property="IsSelected" Value="True">
<!-- This is what I want to do, but ofc this doesn't work because it produces a compile error saying can't set TargetName in a setter -->
<Setter TargetName="parent" Property="SelectedValue" Value="{Binding DataContext, RelativeSource={RelativeSource AncestorType=ListView}}" />
</Trigger>
</Style>
</ListView.Resources>
</ListView>
</DataTemplate>
</Window.Resources>
<ListView ItemsTemplate="{StaticResource parentItemTemplate}" x:Name="parent" SelectedItem="{Binding ParentSelectedItem}" ItemsSource="{Binding Whatever}"/>
</Window>
我该如何完成这项工作?宁愿它在 XAML 中。
【问题讨论】:
-
不能通过 style..但是你为什么要这样做?并且 ListView 没有 IsSelected 属性。你想达到什么目标?
-
@nit:因为这是用户要求。已编辑。