简体   繁体   English

如何定义弹出窗口的位置与ListView中的按钮位置不同? XAML C#WPF

[英]How to define the Location of a Popup to be different from the Button Location in a ListView? XAML C# WPF

I have a ListView of ToggleButtons. 我有一个ToggleButtons的ListView。 Every button is binded to a Popup window. 每个按钮都绑定到一个弹出窗口。 Every Popup window is being opened at the same place as the button, but I want it to be at the same place as the first button. 每个弹出窗口都在与按钮相同的位置打开,但我希望它与第一个按钮位于同一位置。 Here is the code and images: 这是代码和图像:

The buttons: 按钮: 在此处输入图片说明

This is what happens when the first button is open: 这是第一个按钮打开时发生的情况: 在此处输入图片说明

This is what happens when the second button is open: 这是第二个按钮打开时发生的情况: 在此处输入图片说明

<ListView x:Name="ListOfRecipes" HorizontalAlignment="Center" VerticalAlignment="Top" ItemsSource="{Binding}" Grid.Row="1" Margin="25,0.333,25,35" ScrollViewer.VerticalScrollMode="Enabled" Grid.RowSpan="5" >
        <ListView.ItemTemplate>
            <DataTemplate>
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="90*" />
                        <RowDefinition Height="150*" />
                        <RowDefinition Height="0*" />
                    </Grid.RowDefinitions>
                    <ToggleButton x:Name="RecipeButton" Grid.Row="1" BorderBrush="#FF65C365" VerticalAlignment="Center" HorizontalAlignment="Center" Click="Button_Click" Height="150" Width="328" >
                        <StackPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center" Height="128" Width="328">
                            <Image Source="{Binding Path=ImageUri}" Height="128" Width="128" Margin="0,6,0,-5.667" />
                            <StackPanel Orientation="Vertical" HorizontalAlignment="Left" VerticalAlignment="Top" Height="128" Width="192">
                                <TextBlock Height="25" Width="190" Foreground="#FF6FDC13" Text="{Binding Name}"  VerticalAlignment="Top" />
                                <Image Name="YesOrNoImage" Source="{Binding Path=YesOrNoImage}" Width="102" Height="102" HorizontalAlignment="Center" VerticalAlignment="Bottom"/>
                            </StackPanel>
                        </StackPanel>
                    </ToggleButton>
                    <Popup IsOpen="{Binding IsChecked, ElementName=RecipeButton, Mode=TwoWay}" Height="0" Width="328" VerticalAlignment="Center" Name="PopupOne" IsLightDismissEnabled="True" IsHoldingEnabled="False" ScrollViewer.VerticalScrollMode="Enabled" Visibility="{Binding IsChecked, ElementName=RecipeButton}">
                        <Border BorderBrush="#FF65C365" BorderThickness="1" Background="White" Height="514" Width="328">
                            <ScrollViewer VerticalScrollMode="Enabled" >

                                <StackPanel Orientation="Vertical" ScrollViewer.VerticalScrollMode="Enabled">
                            <Image Source="{Binding Path=ImageUri}" Height="328" Width="328" />
                            <TextBlock Foreground="#FF6FDC13" Text="{Binding Name}" HorizontalAlignment="Left" FontSize="28" />
                                    <TextBlock Foreground="Black" Text="{Binding RecipeText}" HorizontalAlignment="Left" FontSize="18" />
                        </StackPanel>
                            </ScrollViewer>

                        </Border>
                </Popup>
                </Grid>
            </DataTemplate> 
        </ListView.ItemTemplate>
    </ListView>

In the popup you can define a grid and set the popup height there Something like this: 在弹出窗口中,您可以定义一个网格并在那里设置弹出窗口的高度,如下所示:

<Popup x:Name="resultsPopup"
                       AllowsTransparency="True"
                       IsOpen="{Binding IsResultsPopupOpen,
                                        Mode=TwoWay,
                                        UpdateSourceTrigger=PropertyChanged}"
                       Placement="Bottom"
                       PlacementTarget="{Binding ElementName=SearchBox}"
                       StaysOpen="False">
                    <Grid Width="{Binding ActualWidth,
                                          ElementName=SearchBox}"
                          Height="300">


                    </Grid>
                </Popup>

UPDATE: 更新:

You can place the popup wherever you want with the Placement property as shown above, the set the binding of the "PlacementTarget" to the control you want to bind. 您可以使用如上所示的Placement属性将弹出窗口放置在所需的位置,将“ PlacementTarget”的绑定设置为要绑定的控件。 In the example above the popup will be shown below the UI control named SearchBox 在上面的示例中,弹出窗口将显示在名为SearchBox的UI控件下方。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM