問題描述
如何使 WPF 文本框填充兩個按鈕之間的所有可用空間? (How to make a WPF TextBox fill all available space between two buttons ?)
I'm trying to make a WPF TextBox fill all available space between two buttons. For some reason the code below does not what I'm trying to achieve
<DockPanel Height="48" LastChildFill="False">
<Button DockPanel.Dock="Left">
<Image Source="Images\large_load.png"></Image>
</Button>
<Button DockPanel.Dock="Left">
<Image Source="Images\large_reload.png"></Image>
</Button>
<TextBox Height="24" HorizontalAlignment="Stretch" DockPanel.Dock="Left"></TextBox>
<Button DockPanel.Dock="Right" Width="48">
<Image Source="Images\large_delete.png"></Image>
</Button>
</DockPanel>
The TextBox is not stretched.
Another problem is that when text is added, the textbox width increases and eventually it pushes the right button out of the visible space.
‑‑‑‑‑
參考解法
方法 1:
Don't set LastChildFill
to false and make the TextBox the last child (by moving the element to the bottom in the code).
(Or use a proper control, like a Grid
)