3 回答

TA貢獻(xiàn)1842條經(jīng)驗(yàn) 獲得超13個(gè)贊
而不是這個(gè):
<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>
嘗試這個(gè):
<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>
我刪除了最后一個(gè)按鈕寬度屬性。

TA貢獻(xiàn)1798條經(jīng)驗(yàn) 獲得超3個(gè)贊
我猜你正在嘗試制作 3 列布局,兩側(cè)各有 1 個(gè)按鈕,中間有 1 個(gè)內(nèi)容通道。
因此,您可以嘗試這樣的操作,其中所有內(nèi)容都位于具有不同 z-index 和水平對齊的同一網(wǎng)格中,現(xiàn)在,當(dāng)您調(diào)整窗口大小時(shí),標(biāo)簽保持在中間,按鈕“夾在”標(biāo)簽內(nèi)容的右側(cè)和后面左邊。
<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貢獻(xiàn)1804條經(jīng)驗(yàn) 獲得超8個(gè)贊
在 DockPanel 中,HorizontalAlignment 僅當(dāng) DockPanel 的寬度大于其所有子項(xiàng)的總寬度時(shí)才有用。按鈕和標(biāo)簽的大小固定為 200 和 150,我相信 WPF 的行為將從左側(cè)剪切元素。
第一個(gè)棘手的方法是讓 DockPanel 填充兩列,當(dāng)網(wǎng)格變小時(shí),DockPanel 的一部分將進(jìn)入第一列的內(nèi)容下方,給人一種從左側(cè)剪切的感覺。
<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>
另一種正確的方法是使用另一個(gè) 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 關(guān)注
- 171 瀏覽
添加回答
舉報(bào)