如果您确实想使用同步,您需要绑定到集合的当前项目,该项目将由 ListBox 或任何其他已IsSynchronizedWithCurrentItem
设置为的控件设置true
,这样做使用/
:
<TextBlock Text="{Binding Model.BinariesToDeploy/}" />
当源是集合视图时,可以用斜杠 (/) 指定当前项。例如,子句 Path=/ 将绑定设置为视图中的当前项。当源是集合时,此语法指定默认集合视图的当前项。
当前项目由CollectionView
原始集合之上的一层管理,CollectionViews 也可用于过滤、排序和分组。
一个例子:
<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:sys="clr-namespace:System;assembly=mscorlib" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Page.Resources>
<x:Array x:Key="items" Type="{x:Type Label}">
<Label Content="Apple" Tag="Fruit"/>
<Label Content="Pear" Tag="Fruit"/>
<Label Content="Orange" Tag="Fruit"/>
<Label Content="Lime" Tag="Fruit"/>
<Label Content="Tomato" Tag="Vegetable"/>
<Label Content="Radish" Tag="Vegetable"/>
<Label Content="Lettuce" Tag="Vegetable"/>
</x:Array>
</Page.Resources>
<ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">
<StackPanel>
<ListBox IsSynchronizedWithCurrentItem="True" ItemsSource="{Binding Source={StaticResource items}}"/>
<!-- Binds to CurrentItem.Content -->
<ContentControl Content="{Binding /Content,Source={StaticResource items}}"/>
</StackPanel>
</ScrollViewer>
</Page>