WPF Dockpanel 第一个子项使用剩余空间
在我的窗口中,有一个 DockPanel 列表来指定几个文件。每个 DockPanel 都有一个文本框(用于路径)和一个按钮(用于浏览文件)。
我重新创建了一个简单的 WPF 页面来演示这里的问题:
<Page
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="150"
Height="22">
<DockPanel>
<TextBox HorizontalAlignment="Stretch"/> <!-- path to file -->
<Button Content="..." DockPanel.Dock="Right"/> <!-- button to browse for file -->
</DockPanel>
</Page>
问题是我希望按钮位于文本框的右侧,但这会导致文本框非常小,因为 DockPanel 的 LastChild 是它用完的按钮剩余空间。我尝试通过将它们打乱并设置 LastChildFill="False"
来改变这一点,但这只会导致按钮再次变小,而不会使文本框变宽(即使使用 HorizontalAlignment="Stretch" )。
我希望控件按该顺序排列的原因是,当使用 tab
在窗口中导航时,我希望用户先到达 TextBox,然后再到达 Button。我研究了设置 TabIndex
,但感觉很老套,WPF 最喜欢的功能是 tabindex 是按照 XAML 中定义的控件的顺序排列的。更不用说我可能必须在窗口中的所有内容上手动设置 TabIndex。
对我来说,似乎 TextBox.HorizontalAlignment 的设置不受尊重。 如何使第一个控件使用尽可能多的空间,但仍保留 Tab 键顺序?
In a window I have there is a list of DockPanel
s to specify a couple of files. Each DockPanel has a TextBox (for the path) and a button (to browse for a file).
I've recreated a simple WPF page to demostrate the problem here:
<Page
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="150"
Height="22">
<DockPanel>
<TextBox HorizontalAlignment="Stretch"/> <!-- path to file -->
<Button Content="..." DockPanel.Dock="Right"/> <!-- button to browse for file -->
</DockPanel>
</Page>
The problem is that I want the button to the right of the textbox, but that causes the textbox to be really small since the LastChild of the DockPanel is the button it uses up the remaining space. Ive tried to change this by shuffling them around and setting LastChildFill="False"
but that only causes the button to be small again, not making the TextBox wide (even with HorizontalAlignment="Stretch"
).
The reason I want the controls in that order is I want the user to reach the TextBox before the Button when using tab
to navigate in the window. I looked into setting TabIndex
but it feels hacky, favorite features of WPF is that tabindex are in the order the conrols are defined in the XAML. Not to mention that I probably would have to manually set TabIndex on everything in the Window.
To me, it seems that the setting of TextBox.HorizontalAlignment isn't respected. How can I make the first control use as much space as it can but still preserve tab order?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
让它像这样:
Make it like this:
如果您不想要 DockPanel 的行为,请不要使用 DockPanel。
If you don't want the behavior of DockPanel, don't use a DockPanel.