考慮以下用于在單擊主視窗中的按鈕后顯示子視窗的代碼隱藏。期望的結果是讓子視窗位置顯示在選單按鈕的右側,與選單按鈕的頂部對齊。(因此該視窗顯示在激活它的控制元件旁邊。)
private void btnMenu2_Click(object sender, RoutedEventArgs e)
{
var menu2 = new Menu2Window();
//var winLocation = this.btnMenu2.TranslatePoint(new Point(0, 0), Application.Current.MainWindow);
//var winLocation = this.btnMenu2.TranslatePoint(new Point(0, 0), this.spLeftMenu);
//var winLocation = this.spLeftMenu.TranslatePoint(new Point(0, 0), this.btnMenu2);
var objLocation = this.spLeftMenu.TranslatePoint(new Point(0, 0), this.spLeftMenu);
var scnLocation = this.btnMenu2.PointToScreen(objLocation);
//menu2.Left = scnLocation.X btnMenu2.Width;
menu2.Left = scnLocation.X 50; // <- Why does this work but using btnMenu2.Width causes placement to be all over the place???
menu2.Top = scnLocation.Y;
menu2.ShowDialog();
}
所呈現的代碼確實以所需的方式作業,但是我不喜歡在代碼中使用硬編碼值或幻數。
如果您使用硬編碼控制元件寬度值 (50) 注釋掉該行并使用按鈕的 width 屬性取消注釋該行,則后續執行會導致選單視窗顯示在違反邏輯的位置序列中。它似乎是從亂數生成器而不是控制元件的寬度獲取值。我確實在多次運行時看到了一個模式,但是由于每次執行代碼時屬性值回應的變化而獲得 4 或 5 個不同的位置是非常令人沮喪的。
這里正確或正確的方法是什么?如何從 WPF 控制元件屬性中獲得可靠的值,或者我要求太多?
主視窗的 XAML:
<Window x:Class="LocateTest.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:LocateTest"
mc:Ignorable="d"
Background="DarkGray"
Title="MainWindow" Height="400" Width="750">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="50" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<StackPanel x:Name="spLeftMenu"
DataContext="MainWindow"
Orientation="Vertical"
VerticalAlignment="Center"
HorizontalAlignment="Center"
Height="325" Width="50">
<Button Content="B1" Height="50" Width="50"/>
<Button x:Name="btnMenu2"
Content="B2"
Height="50"
Click="btnMenu2_Click"/>
<Button Content="B3" Height="50"/>
</StackPanel>
</Grid>
</Window>
Menu2 視窗的 XAML:
<Window x:Class="LocateTest.Menu2Window"
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:LocateTest"
mc:Ignorable="d"
WindowStyle="None"
AllowsTransparency="True"
WindowStartupLocation="Manual"
Height="150" Width="280">
<Window.Background>
<SolidColorBrush Opacity="0.5" Color="Black"></SolidColorBrush>
</Window.Background>
<Grid>
<Button x:Name="btnClose"
Click="btnClose_Click"
Width="25"
Height="25"
Content="X"
Background="Black"
Foreground="Red" Margin="245,10,10,115">
</Button>
</Grid>
</Window>```
uj5u.com熱心網友回復:
由于您沒有明確設定 的寬度btnMenu2,因此其Width屬性的值將為double.NaN。請注意,該Width值不是實際寬度,而是請求的寬度。改用ActualWidthproperty:
menu2.Left = scnLocation.X btnMenu2.ActualWidth;
uj5u.com熱心網友回復:
改用 Popup 怎么樣?您可以將所有您想要的放在 Popup 控制元件而不是視窗上,并將其 StaysOpen 屬性設定為 true 以使用戶通過關閉按鈕或其他方式自動關閉它。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/351759.html
上一篇:子選單彈出背景要透明
