如果未通过其他方式使用嵌套的属性,则可以使用转换器将其绑定到其父级。请注意,父列表视图上的 对于避免转换器的双重选择非常重要。
SelectedItemListViewListViewItem.IsSelectedSelectionMode="Single"
xaml界面:
<Grid x:Name="grid1">
<Grid.Resources>
<local:SelectedItemTransferConverter x:Key="ItemSelectedConverter"/>
</Grid.Resources>
<ListView ItemsSource="{Binding listsToDisplay}" SelectionMode="Single">
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding listName}"/>
<ListView
ItemsSource="{Binding listContent}"
SelectedItem="{Binding IsSelected,RelativeSource={RelativeSource AncestorType={x:Type ListViewItem}},Converter={StaticResource ItemSelectedConverter}}"/>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</Grid>
转换器代码:
public class SelectedItemTransferConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value is bool || value is bool?)
{
if ((bool?)value == false)
{
return null;
}
}
return Binding.DoNothing;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (targetType == typeof(bool) || targetType == typeof(bool?))
{
return value != null;
}
return Binding.DoNothing;
}
}