3 回答

TA貢獻1842條經驗 獲得超13個贊
而不是這個:
<Window x:Class="WpfApp2.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:WpfApp2"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800" >
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" MinWidth="60"/>
<ColumnDefinition Width="auto"/>
</Grid.ColumnDefinitions>
<DockPanel HorizontalAlignment="Right" Grid.Column="1">
<Button Click="Button_Click" Content="click me" Width="150" DockPanel.Dock="Right" />
<Label Content="abcdef" Width="200" DockPanel.Dock="Right" />
<Label x:Name="mLog"/>
</DockPanel>
<Button Click="Button_Click" DockPanel.Dock="Right" Content="click me" Width="150"/>
<Label Content="abcdef" Width="200" DockPanel.Dock="Right"/>
</Grid>
</Window>
嘗試這個:
<Window x:Class="WpfApp2.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:WpfApp2"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800" >
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" MinWidth="60"/>
<ColumnDefinition Width="auto"/>
</Grid.ColumnDefinitions>
<DockPanel HorizontalAlignment="Right" Grid.Column="1">
<Button Click="Button_Click" Content="click me" Width="150" DockPanel.Dock="Right" />
<Label Content="abcdef" Width="200" DockPanel.Dock="Right" />
<Label x:Name="mLog"/>
</DockPanel>
<Button Click="Button_Click" DockPanel.Dock="Right" Content="click me"/>
<Label Content="abcdef" Width="200" DockPanel.Dock="Right"/>
</Grid>
</Window>
我刪除了最后一個按鈕寬度屬性。

TA貢獻1798條經驗 獲得超3個贊
我猜你正在嘗試制作 3 列布局,兩側各有 1 個按鈕,中間有 1 個內容通道。
因此,您可以嘗試這樣的操作,其中所有內容都位于具有不同 z-index 和水平對齊的同一網格中,現在,當您調整窗口大小時,標簽保持在中間,按鈕“夾在”標簽內容的右側和后面左邊。
<Grid>
<Button HorizontalAlignment="Left" Content="click me" Width="150" />
<Button HorizontalAlignment="Right" Content="click me" Width="150" />
<Label HorizontalAlignment="Center" Content="abcdef" Width="200" Background="White" />
</Grid>

TA貢獻1804條經驗 獲得超8個贊
在 DockPanel 中,HorizontalAlignment 僅當 DockPanel 的寬度大于其所有子項的總寬度時才有用。按鈕和標簽的大小固定為 200 和 150,我相信 WPF 的行為將從左側剪切元素。
第一個棘手的方法是讓 DockPanel 填充兩列,當網格變小時,DockPanel 的一部分將進入第一列的內容下方,給人一種從左側剪切的感覺。
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" MinWidth="60"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<DockPanel HorizontalAlignment="Right" Grid.ColumnSpan="2" LastChildFill="True">
<Button Click="Button_Click" Content="click me 2" Width="150" DockPanel.Dock="Right" />
<Label Content="abcdef 2" Width="200" />
<Label x:Name="mLog" DockPanel.Dock="Right"/>
</DockPanel>
<Button Click="Button_Click" Content="click me 1" HorizontalContentAlignment="Stretch"/>
<Label Content="abcdef 1" HorizontalContentAlignment="Stretch"/>
</Grid>
另一種正確的方法是使用另一個 Grid 而不是 DockPanel,確保我們讓第一列填充空間,以便與容器一起減小尺寸。
<Grid HorizontalAlignment="Right" Grid.Column="1">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="150"/>
</Grid.ColumnDefinitions>
<Button Click="Button_Click" Content="click me" Grid.Column="1" MinWidth="150"/>
<Label Content="abcdef" Grid.Column="0"/>
<Label x:Name="mLog"/>
</Grid>
- 3 回答
- 0 關注
- 177 瀏覽
添加回答
舉報