uwp - 弹出窗口离开屏幕。定位应该怎么做?
问题描述
我正在创建一个用户控件。在用户控件中单击按钮时,我希望它打开一个将进行设置的弹出窗口。我可以打开 PopUp,但无法定位它。如果按钮靠近屏幕边缘,则弹出窗口会离开屏幕并且不会出现。例如,如果它在底部,我希望它向上打开。我试图用下面的示例图片来解释它。
如果你能帮忙,我会很高兴。谢谢。
用户控制代码
<Grid Height="60" Width="140">
<Grid.RowDefinitions>
<RowDefinition Height="1*"/>
<RowDefinition Height="5"/>
<RowDefinition Height="2*"/>
<RowDefinition Height="5"/>
<RowDefinition Height="5"/>
</Grid.RowDefinitions>
<controls:DropShadowPanel Name="ShadowEffect" Grid.RowSpan="5" BlurRadius="10.0" ShadowOpacity="1" Color="Black">
<Border Background="White" Width="140">
</Border>
</controls:DropShadowPanel>
<TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" FontWeight="SemiBold" FontSize="14" Text="{Binding ID}" TextAlignment="Center" Margin="0,4,0,0"/>
<CheckBox Name="LockerCheck" Background="Blue" IsChecked="{Binding IsChecked}" Margin="5,-2,0,0" Checked="LockerCheck_Checked" Unchecked="LockerCheck_Unchecked" Grid.RowSpan="3" VerticalAlignment="Top">
<CheckBox.RenderTransform>
<CompositeTransform ScaleX="0.7" ScaleY="0.7"/>
</CheckBox.RenderTransform>
</CheckBox>
<Button Name="LockerButton" Grid.Row="2" Height="27" Width="70" Background="#FFCF0000" BorderBrush="Transparent" HorizontalAlignment="Center" VerticalAlignment="Top" Click="LockerButton_Click" Margin="0,0,0,0">
<Image Name="LockerIcon" Source="/Images/lock.png"/>
</Button>
<Rectangle Name="LockerStatusColor" Grid.Row="4" Fill="#FFCF0000"/>
<Button Name="PopUpButton" Content="O" HorizontalAlignment="Right" Click="PopUpButton_Click">
</Button>
</Grid>
<Popup x:Name="myPopup" IsLightDismissEnabled="True" HorizontalAlignment="Center" VerticalAlignment="Center" >
<StackPanel Orientation="Horizontal" Width="250" Height="400" Background="Red">
<TextBlock Text="POPUP TEXT" Width="100"></TextBlock>
<Button Content="POPUP BUTTON"></Button>
</StackPanel>
</Popup>
解决方案
弹出窗口离开屏幕。定位应该怎么做?
对于这种情况,您需要获取转换为当前窗口的Popup
内容边界。然后计算超出部分,并将弹出窗口设置 HorizontalOffset
VerticalOffset
为正确位置。
例如给出弹出内容x:Name = PopContent
。
<Popup
x:Name="myPopup"
HorizontalAlignment="Center"
VerticalAlignment="Center"
IsLightDismissEnabled="True"
IsOpen="True"
LayoutUpdated="myPopup_LayoutUpdated">
<StackPanel
x:Name="PopContent"
Width="250"
Height="400"
Background="Red"
Orientation="Horizontal">
<TextBlock Width="100" Text="POPUP TEXT" />
<Button Content="POPUP BUTTON" />
</StackPanel>
</Popup>
然后使用以下代码在LayoutUpdated
事件中正确弹出窗口的位置。
private void myPopup_LayoutUpdated(object sender, object e)
{
Rect bounds = PopContent.TransformToVisual(Window.Current.Content).TransformBounds(new Rect(0.0, 0.0, PopContent.ActualWidth, PopContent.ActualHeight));
Rect rect = new Rect(0.0, 0.0, Window.Current.Bounds.Width, Window.Current.Bounds.Height);
if (!rect.Contains(new Point(bounds.Left, bounds.Top)))
{
if (bounds.Top < 0)
{
myPopup.HorizontalOffset = -(bounds.Top);
}
if (bounds.Left < 0)
{
myPopup.VerticalOffset = -(bounds.Left);
}
}
if (!rect.Contains(new Point(bounds.Right, bounds.Bottom)))
{
if (bounds.Bottom > rect.Height)
{
myPopup.VerticalOffset = -(bounds.Bottom - rect.Height);
}
if (bounds.Right > rect.Width)
{
myPopup.HorizontalOffset = -(bounds.Right - rect.Width);
}
}
}
推荐阅读
- javascript - 如何在我的对象标签的内屏幕上获得与输入网站控制台命令相同的效果?
- javascript - Quasar:当我单击其中一个菜单时,drawerClick 不起作用
- sql - 查询每个品种每分钟数据的差异
- assembly - 仅装配数字输入
- xml - 无法为安装 WiX 设置自定义位置。安装后,它只安装在 C\programfiles86 中。它只有 bundle.wxs 没有 product.wxs
- python - 如何在Android设备的下载文件夹中找到文件的完整路径(然后使用python枕头打开)?
- vmware - 在 AMD Ryzen 5 3500 上尝试 ESXi 6 / 6.5 时发现错误“无网络适配器”
- gcc - 测量全局变量的内存使用情况
- html - 我不知道如何将图像放在边框内
- python - 两条迹线,但只有一条由滑块控制