如何在全屏窗口的画布中定位 WPF 控件?



我目前正在开发一个全屏WPF应用程序。应用在Window_Loaded活动中全屏显示。

在窗户里,我有一个画布。在画布中,我有一个媒体元素和 3 张图像。下面是 XAML:

<Window x:Class="Test_CS4.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Test_CS4"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800" Loaded="Window_Loaded" AllowDrop="True" Background="Black">
<Canvas x:Name="myCanvas" HorizontalAlignment="Center" VerticalAlignment="Center" UseLayoutRounding="True" >
<MediaElement x:Name="mediaElement1" MediaEnded="mediaElement_OnMediaEnded" Canvas.ZIndex="1" Height="390" VerticalAlignment="Center" MouseLeftButtonDown="mediaElement1_MouseLeftButtonDown_1" HorizontalAlignment="Center" Width="767" Canvas.Left="-384" Canvas.Top="-190"/>
<Image x:Name="image1" Canvas.ZIndex="2" Height="390"  Width="791" HorizontalAlignment="Center" VerticalAlignment="Center" Canvas.Left="-795" Canvas.Top="-392" />
<Image x:Name="checkImg" HorizontalAlignment="Left" Height="54" Margin="50,196,0,0" VerticalAlignment="Top" Width="67"/>
<Image x:Name="crossImg" Height="54" Margin="0,196,663,0" VerticalAlignment="Top" MouseLeftButtonDown="crossImg_MouseLeftButtonDown" HorizontalAlignment="Right" Width="67"/>
</Canvas>
</Window>

问题

当我运行应用程序时,画布无法更新和适合整个全屏。因此,当我尝试为图像设置位置时(例如,使用Canvas.SetLeft(this.crossImg,0((时,当窗口全屏显示时,画布实际上被移动了。

我需要设置图像的"x y 坐标"并相应地更改它们。我尝试过使用网格,但它不允许我在 MediaElement 顶部叠加图像,所以我明白画布是要走的路。

问题

当窗口进入全屏模式时,如何以编程方式调整画布大小以适合窗口?

您可以使用标签 这将为您提供填充拉伸等选项

最新更新