簡體   English   中英

將彈出PlacementTarget綁定到列表框SelectedItem

[英]Binding popup PlacementTarget to listbox SelectedItem

我有以下XAML(簡體):

<Grid>
        <Popup Name="Popup" DataContext="{Binding ElementName=GameWheel, Path=SelectedItem}" PlacementTarget="{Binding SelectedItem}" Placement="Center" IsOpen="True">
            <Image Source="{Binding ImagePath}" Width="100" />
        </Popup>

        <DockPanel Margin="40,0">
            <ListBox x:Name="GameWheel"  ...>
                <ListBox.ItemsPanel>
                    <ItemsPanelTemplate>
                        <c:VirtualizingWrapPanel IsItemsHost="True" />
                    </ItemsPanelTemplate>
                </ListBox.ItemsPanel>
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <Image x:Name="GamesWheelImage" ... />
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
        </DockPanel>
    </Grid>

雖然DataContext是正確的,並且可以在彈出窗口中顯示正確的圖像,但放置位置完全錯誤。 我的意圖是使彈出窗口覆蓋列表框中SelectedItem的中心,但是由於某些原因,它始終位於窗口的中心。

關於如何在列表框中獲取selectedItem的位置的任何想法,以便彈出窗口始終覆蓋所選項目的中心? 我得到的最接近的圖像將其放置在列表框的中央,但是我無法使其僅覆蓋所選的項目。

在我看來,您應該為屬於Listbox的ListBoxItems創建一個特定的模板,稱為GameWheel 為了給您一個示例,我創建了ListBox的簡化版本:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:sys="clr-namespace:System;assembly=mscorlib"
        Title="MainWindow" Height="300" Width="400">

    <Window.Resources>
        <ControlTemplate x:Key="ListBoxItemWithPopup" TargetType="{x:Type ListBoxItem}">
            <Border Name="border" Padding="2">
                <Grid>
                    <Popup Name="popup" PlacementTarget="{Binding RelativeSource={RelativeSource TemplatedParent}}" 
                           Placement="Center" >
                        <TextBlock Margin="1" Background="White" Foreground="Black" Text="{TemplateBinding Content}" />
                    </Popup>
                    <ContentPresenter />
                </Grid>
            </Border>
            <ControlTemplate.Triggers>
                <Trigger Property="IsSelected" Value="true">
                    <Setter TargetName="popup" Property="IsOpen" Value="True"/>
                    <Setter TargetName="border" Property="Background" Value="Azure" />
                </Trigger>
            </ControlTemplate.Triggers>
        </ControlTemplate>
    </Window.Resources>

    <Grid>

        <DockPanel Margin="40,0">
            <ListBox x:Name="GameWheel">
                <ListBox.ItemContainerStyle>
                    <Style TargetType="{x:Type ListBoxItem}">
                        <Setter Property="Template" Value="{StaticResource ListBoxItemWithPopup}" />
                    </Style>
                </ListBox.ItemContainerStyle>
                <ListBox.Items>
                    <sys:String>One</sys:String>
                    <sys:String>Two</sys:String>
                    <sys:String>Three</sys:String>
                    <sys:String>Four</sys:String>
                </ListBox.Items>
            </ListBox>
        </DockPanel>
    </Grid>

</Window>

如您所見, Popup直接包含在ListBoxItem模板中,並使用觸發器顯示。 希望我的樣品能對您有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM