将弹出窗口保持在窗口范围内

Keep popup within window bounds(将弹出窗口保持在窗口范围内)
本文介绍了将弹出窗口保持在窗口范围内的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在按钮下显示一个弹出窗口.

I'm trying to display a popup under a button.

这就是现在的样子:

但我希望它保持在窗口边界内,就像这样(油漆中的示例)

But I want it to stay within the window borders, something like this (example in paint)

这是我的顶部栏,底部声明了弹出窗口:

This is my top bar with the popup declared at the bottom:

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="55" />
        <RowDefinition Height="35" />
    </Grid.RowDefinitions>

    <Grid x:Name="AdminContainer" Grid.Row="0">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="4*" />
            <ColumnDefinition Width="10" />
            <ColumnDefinition Width="Auto" />
        </Grid.ColumnDefinitions>

        <Border Background="Transparent">
            <Viewbox StretchDirection="DownOnly" Stretch="Uniform" Height="35" HorizontalAlignment="Left" Margin="10,0,0,0">
                <ContentControl Content="{StaticResource LynxLogo}" Margin="0,5" />
            </Viewbox>
        </Border>

        <ToggleButton x:Name="AdminOptionsToggleButton" 
                      Grid.Column="2" 
                      VerticalAlignment="Center"
                      HorizontalAlignment="Stretch"
                      IsChecked="{Binding mainViewModel.OptionsPopupOpen, Mode=TwoWay}"
                      Style="{StaticResource EmptyToggleButtonStyle}"
                      Cursor="Hand">

            <Grid x:Name="AdminPanel" HorizontalAlignment="Right" Height="45"
                  VerticalAlignment="Stretch" Margin="0,0,2,0" >
                <Grid.Style>
                    <Style>
                        <Setter Property="Grid.Background" Value="Transparent"/>
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType=ToggleButton}, Path=IsMouseOver}" Value="True">
                                <Setter Property="Grid.Background" Value="#FF404040"/>
                            </DataTrigger>
                            <DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType=ToggleButton}, Path=IsChecked}" Value="True">
                                <Setter Property="Grid.Background" Value="#FF353535"/>
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </Grid.Style>

                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="45" />
                </Grid.ColumnDefinitions>


                <Border Grid.Column="0" Padding="20,0,0,0">
                    <Grid>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="*" />
                            <RowDefinition Height="*" />
                        </Grid.RowDefinitions>

                        <!--Name-->
                        <TextBlock Grid.Row="0" Text="{Binding adminViewModel.Name}"
                                   Style="{StaticResource AdminTextBlockStyle}" VerticalAlignment="Bottom"
                                   FontSize="16" />
                        <!--Status-->
                        <TextBlock Grid.Row="1" Text="{Binding adminViewModel.StatusDescription}"
                                   Style="{StaticResource AdminTextBlockStyle}" VerticalAlignment="Bottom"
                                   FontSize="13" Margin="0,0,0,1" />
                    </Grid>
                </Border>

                <Border Grid.Column="1" HorizontalAlignment="Right" Margin="3">
                    <Viewbox StretchDirection="DownOnly">
                        <Image x:Name="ProfilePicture" Source="/Image/ProfilePictures/profile_placeholder.png" />
                    </Viewbox>
                </Border>
            </Grid>
        </ToggleButton>
    </Grid>

    <views:OptionPopup x:Name="OptionsMenu" PlacementTarget="{Binding ElementName=AdminPanel}"
                       Placement="Bottom" 
                       MinWidth="{Binding ActualWidth, ElementName=AdminPanel}"
                       MaxWidth="{Binding ActualWidth, ElementName=AdminContainer}" />

推荐答案

我通过设置自定义放置来修复它:

I fixed it by setting the placement the custom:

<views:OptionPopup x:Name="OptionsMenuPopup"
     PlacementTarget="{Binding ElementName=AdminPanel}"
     Placement="Custom"
     MinWidth="{Binding ActualWidth, ElementName=AdminPanel}"
     MaxWidth="{Binding ActualWidth, ElementName=AdminContainer}" /> 

在我的用户控件的构造函数中,我在后面的代码中添加了

And in the constructor of my usercontrol in code behind I added

OptionsMenuPopup.CustomPopupPlacementCallback += (Size popupSize, Size targetSize, Point offset) =>
            new[] { new CustomPopupPlacement() { Point = new Point(targetSize.Width - popupSize.Width, targetSize.Height) } }; 

这篇关于将弹出窗口保持在窗口范围内的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)