WPF Dockpanel 第一个子项使用剩余空间

发布于 2024-09-18 14:20:56 字数 1074 浏览 8 评论 0原文

在我的窗口中,有一个 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" 来改变这一点,但这只会导致按钮再次变小,而不会使文本框变宽(即使使用 Horizo​​ntalAlignment="Stretch" )。

我希望控件按该顺序排列的原因是,当使用 tab 在窗口中导航时,我希望用户先到达 TextBox,然后再到达 Button。我研究了设置 TabIndex ,但感觉很老套,WPF 最喜欢的功能是 tabindex 是按照 XAML 中定义的控件的顺序排列的。更不用说我可能必须在窗口中的所有内容上手动设置 TabIndex。

对我来说,似乎 TextBox.Horizo​​ntalAlignment 的设置不受尊重。 如何使第一个控件使用尽可能多的空间,但仍保留 Tab 键顺序?

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

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

幼儿园老大 2024-09-25 14:20:56

让它像这样:

<DockPanel LastChildFill="True">
    <Button Content="..." DockPanel.Dock="Right"/> <!-- button to browse for file -->
    <TextBox DockPanel.Dock="Left" HorizontalAlignment="Stretch"/> <!-- path to file -->
</DockPanel>

Make it like this:

<DockPanel LastChildFill="True">
    <Button Content="..." DockPanel.Dock="Right"/> <!-- button to browse for file -->
    <TextBox DockPanel.Dock="Left" HorizontalAlignment="Stretch"/> <!-- path to file -->
</DockPanel>
谜兔 2024-09-25 14:20:56

如果您不想要 DockPanel 的行为,请不要使用 DockPanel。

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

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

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文