我越來越接近對System.Windows.Controls.UserControl物件定位的理解:
默認情況下,相對于它們的容器,它們沒有 X 或 Y 坐標,但可以使用所謂的“附加屬性”添加一些坐標.
此類附加屬性的典型示例是Canvas.Leftand Canvas.Top,這意味著,如果 的容器UserControl是 a Canvas,則(假設)其左上角(偽代碼)會發生以下情況:
UserControl_UpperLeft_Point.X = Canvas.Left
UserControl_UpperLeft_Point.Y = Canvas.Top
現在我想知道的是:
我的想法正確嗎?確實是使用的左上角嗎?
如果我想修改這種行為怎么辦,讓我們說:
int left_Margin = 100; int top_Margin = 200; UserControl_UpperLeft_Point.X = Canvas.Left / 2 left_Margin; UserControl_UpperLeft_Point.Y = Canvas.Top * 2 top_Margin;如果我想
UserControl根據右上角甚至中心定位我的 , 怎么辦?
uj5u.com熱心網友回復:
我的想法正確嗎?確實是使用的左上角嗎?
在這種情況下,它實際上是左上角,但實際上每個邊都是對齊的。此外,控制元件由 對齊Canvas,它沒有設定內部 X 或 Y 坐標。它們由特定于 的附加屬性給出Canvas,沒有其他面板。內部Canvas計算一個矩形,它在其中繪制UserControl.

將其水平和垂直對齊并設定邊距,就是這樣。
<Grid> <Grid.Resources> <Style TargetType="Border"> <Setter Property="CornerRadius" Value="5" /> <Setter Property="Width" Value="50" /> <Setter Property="Height" Value="50" /> <Setter Property="Background" Value="Red" /> <Setter Property="TextElement.Foreground" Value="White" /> <Setter Property="TextElement.FontWeight" Value="Bold" /> </Style> <Style TargetType="TextBlock"> <Setter Property="HorizontalAlignment" Value="Center" /> <Setter Property="VerticalAlignment" Value="Center" /> <Setter Property="TextAlignment" Value="Center" /> </Style> </Grid.Resources> <!--#region top--> <Border Margin="20,20,0,0" HorizontalAlignment="Left" VerticalAlignment="Top"> <TextBlock> Top<LineBreak /> Left</TextBlock> </Border> <Border Margin="0,20,0,0" HorizontalAlignment="Center" VerticalAlignment="Top"> <TextBlock> Top<LineBreak /> Center</TextBlock> </Border> <Border Margin="0,20,20,0" HorizontalAlignment="Right" VerticalAlignment="Top"> <TextBlock> Top<LineBreak /> Right</TextBlock> </Border> <!--#endregion--> <!--#region center--> <Border Margin="20,0,0,0" HorizontalAlignment="Left" VerticalAlignment="Center"> <TextBlock> Center<LineBreak /> Left</TextBlock> </Border> <Border Margin="0,0,0,0" HorizontalAlignment="Center" VerticalAlignment="Center"> <TextBlock> Center<LineBreak /> Center</TextBlock> </Border> <Border Margin="0,0,20,0" HorizontalAlignment="Right" VerticalAlignment="Center"> <TextBlock> Center<LineBreak /> Right</TextBlock> </Border> <!--#endregion--> <!--#region bottom--> <Border Margin="0,0,20,20" HorizontalAlignment="Right" VerticalAlignment="Bottom"> <TextBlock> Bottom<LineBreak /> Right</TextBlock> </Border> <Border Margin="0,0,0,20" HorizontalAlignment="Center" VerticalAlignment="Bottom"> <TextBlock> Bottom<LineBreak /> Center</TextBlock> </Border> <Border Margin="20,0,0,20" HorizontalAlignment="Left" VerticalAlignment="Bottom"> <TextBlock> Bottom<LineBreak /> Left</TextBlock> </Border> <!--#endregion--> </Grid>如果你想擁有舊的風格(從 WinForms 知道你可以對齊它
HorizontalAlignemnt="Top"并VerticalAlignment="Left"使用Leftand的Top屬性Margin來設定舊的學校Left和Top.
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/440625.html上一篇:Cenos7+Git+Docker+Jenkins+Python持續集成:第一章 docker篇
下一篇:如何在WPF中的選單上附加標簽?
