2

我有一个 ListView 绑定到一个ObservableCollection<Foo>. 在选择 ListViewItem 时,我会在容器中显示 SelectedItem(Foo members) 的详细信息。它工作正常。

现在我只想在 SelectedItem 旁边显示详细信息。即,如果我选择第四个 ListViewItem,那么 Container 的 Top 应该与 ListViewItem 的 Top 相同。如果即使在滚动列表时也会产生任何问题,我将如何同步他们的位置。

PS:滚动条被隐藏

这个问题还没有解决。有人可以帮忙吗?

4

1 回答 1

1

Original Answer

Does the detail need to be in a separate container? I may be misunderstanding your example, but I would have thought you could achieve what you wanted by adding the details section to the item template for your list items and then hiding/showing it based on the IsSelected flag:

<ListView ItemsSource="{Binding}">
        <ListView.ItemTemplate>
            <DataTemplate>
                <DockPanel>
                    <ContentControl DockPanel.Dock="Right" Name="DetailsControl" Content="{Binding}" ContentTemplate="{StaticResource DetailsTemplate}" />
                    <TextBlock Text="{Binding}" />
                </DockPanel>
                <DataTemplate.Triggers>
                    <DataTrigger Binding="{Binding (ListViewItem.IsSelected), RelativeSource={RelativeSource FindAncestor, AncestorType=ListViewItem}}" Value="False">
                        <Setter TargetName="DetailsControl" Property="Visibility" Value="Hidden" />
                    </DataTrigger>
                </DataTemplate.Triggers>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>

Even if you aren't after exactly this behaviour, I imagine you could get close to what you want by replacing the ContentControl from the example with something else (e.g. a Popup)


Edit in response to comments

The example below will place a Popup to the right hand side of the ListView that is visible only for the selected item:

<ListView ItemsSource="{Binding}">
    <ListView.ItemTemplate>
        <DataTemplate>
            <DockPanel>
                <TextBlock Text="{Binding}" />

                <Popup Placement="Right"
                        PlacementTarget="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=ListViewItem}}"
                        IsOpen="{Binding (ListViewItem.IsSelected), RelativeSource={RelativeSource FindAncestor, AncestorType=ListViewItem}}">

                    <Border Background="Black" Padding="3">
                        <TextBlock Text="{Binding}" />
                    </Border>
                </Popup>

            </DockPanel>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

This uses the Placement attribute to specify that the Popup should appear to the right of the target element, and binds the PlacementTarget property to the first ancestor of type ListViewItem (i.e. the parent container).

This causes an appearance like the example below:

working example

于 2010-06-24T15:41:37.590 回答