弹出窗口离开屏幕。应该如何定位?
Popup goes off screen. How should positioning be done?
我正在创建一个用户控件。当在用户控件中单击一个按钮时,我希望它打开一个弹出窗口,其中将进行设置。我可以打开 PopUp,但我无法定位它。如果按钮靠近屏幕边缘,则弹出窗口会离开屏幕并且不会出现。例如,如果它在底部,我希望它向上打开。我试着用下面的示例图片来解释它。
如果你能帮上忙,我会很高兴。谢谢
Appropriate Image
Incorrect Image
用户控制代码
<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 goes off screen. How should positioning be done?
对于这种情况,您需要获取 Popup
content 转换为当前 Window 的边界。然后计算beyond部分,将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);
}
}
}
我正在创建一个用户控件。当在用户控件中单击一个按钮时,我希望它打开一个弹出窗口,其中将进行设置。我可以打开 PopUp,但我无法定位它。如果按钮靠近屏幕边缘,则弹出窗口会离开屏幕并且不会出现。例如,如果它在底部,我希望它向上打开。我试着用下面的示例图片来解释它。
如果你能帮上忙,我会很高兴。谢谢
Appropriate Image
Incorrect Image
用户控制代码
<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 goes off screen. How should positioning be done?
对于这种情况,您需要获取 Popup
content 转换为当前 Window 的边界。然后计算beyond部分,将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);
}
}
}