WPF Dockpanel的第一个孩子使用剩余空间 [英] WPF Dockpanel first child uses remaining space

查看:125
本文介绍了WPF Dockpanel的第一个孩子使用剩余空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个窗口中,我有一个DockPanel列表,用于指定几个文件.每个DockPanel都有一个TextBox(用于路径)和一个按钮(用于浏览文件).

In a window I have there is a list of DockPanels to specify a couple of files. Each DockPanel has a TextBox (for the path) and a button (to browse for a file).

我已经重新创建了一个简单的WPF页面以在此处演示问题:

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>

问题是我希望按钮位于文本框的右侧,但这会导致文本框非常小,因为DockPanel的LastChild是按钮,它占用了剩余空间.我试图通过改组并设置LastChildFill="False"来更改此设置,但这只会使按钮再次变小,而不会使TextBox变宽(即使使用HorizontalAlignment="Stretch").

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").

我希望按此顺序进行控制的原因是,我希望用户在使用tab在窗口中导航时在Button之前到达TextBox.我研究了设置TabIndex的情况,但感觉很骇人,WPF最喜欢的功能是tabindex的顺序与XAML中定义的顺序相同.更不用说我可能必须在Window的所有内容上手动设置TabIndex.

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.

在我看来,似乎不尊重TextBox.Horizo​​ntalAlignment的设置. 如何使第一个控件使用尽可能多的空间,但仍保留制表符顺序?

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?

推荐答案

如果您不想使用DockPanel,请不要使用DockPanel.

If you don't want the behavior of DockPanel, don't use a DockPanel.

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition/>
        <ColumnDefinition Width="Auto"/>
    </Grid.ColumnDefinitions>

    <TextBox />
    <Button Content="..." Grid.Column="1"/>
</Grid>

这篇关于WPF Dockpanel的第一个孩子使用剩余空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆