WPF 中的停靠控件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19125362/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 09:46:00  来源:igfitidea点击:

Dock Controls in WPF

c#wpfwinforms

提问by Federico Berasategui

I'm very new to WPF but I'm used to winforms. In winforms when I want to dock a control I just use DockStylebut I don't know how to do it in wpf. From the answer to this question, I have been able to dock my richtextbox in the window.

我对 WPF 很陌生,但我已经习惯了 winforms。在 winforms 中,当我想停靠我刚使用的控件时,DockStyle但我不知道如何在 wpf 中执行此操作。从这个问题的答案,我已经能够将我的 Richtextbox 停靠在窗口中。

But my problem now is how to dock my status bar and menustrip at the top and bottom of the window. I have tried using

但我现在的问题是如何将状态栏和菜单条停靠在窗口的顶部和底部。我试过使用

<Window x:Class="Textpad.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Textpad" Height="324" Width="390" FontFamily="Tahoma">
<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="368*" />
        <ColumnDefinition Width="0*" />
    </Grid.ColumnDefinitions>
    <Menu Height="23" HorizontalAlignment="Center" Name="menu1" VerticalAlignment="Top" Width="368" Grid.ColumnSpan="2">
        <MenuItem Header="File">
            <MenuItem Header="New" />
            <MenuItem Header="Open" />
            <MenuItem Header="Save" />
            <MenuItem Header="Save as" />
            <Separator />
            <MenuItem Header="Print" />
            <MenuItem Header="Print Preview" />
            <MenuItem Header="Page Setup" />
            <Separator />
            <MenuItem Header="Exit" />
        </MenuItem>
        <MenuItem Header="Edit">
            <MenuItem Header="Undo" />
            <MenuItem Header="Redo" />
            <Separator />
            <MenuItem Header="Cut" />
            <MenuItem Header="Copy" />
            <MenuItem Header="Paste" />
            <MenuItem Header="Delete" />
            <Separator />
            <MenuItem Header="Find" />
            <MenuItem Header="Replace" />
            <Separator />
            <MenuItem Header="Select All" />
        </MenuItem>
        <MenuItem Header="Format">
            <MenuItem Header="Word Wrap" />
            <Separator />
            <MenuItem Header="Font" />
            <MenuItem Header="Text Color" />
        </MenuItem>
        <MenuItem Header="View">
            <MenuItem Header="Status Bar" />
        </MenuItem>
        <MenuItem Header="Help">
            <MenuItem Header="About Textpad" />
        </MenuItem>
    </Menu>
    <DockPanel>
        <StatusBar Height="23 " HorizontalAlignment="Stretch" Name="statusBar1" Width="368" Grid.ColumnSpan="2" DockPanel.Dock="Bottom">
            <StatusBarItem Content="This is status baritem content to test" />
        </StatusBar>
    </DockPanel>
    <RichTextBox HorizontalAlignment="Stretch" Margin="0,21" Name="richTextBox1" VerticalAlignment="Stretch" TextChanged="richTextBox1_TextChanged" VerticalContentAlignment="Top" Grid.ColumnSpan="2" />
</Grid>

But the menu and status bar are docked at the center of the window when maximised.

但是菜单和状态栏在最大化时停靠在窗口的中心。

Please what am i doing wrong?

请问我做错了什么?

回答by Federico Berasategui

You need a DockPaneland set the DockPanel.DockAttached Property to the elements you want to dock:

您需要DockPanelDockPanel.Dock附加属性设置为要停靠的元素:

<Window>
   <DockPanel>
      <StatusBar Height="23" DockPanel.Dock="Bottom"/>
      <Menu Height="23" DockPanel.Dock="Top"/>

      <!-- Main Window Content here -->
   </DockPanel>
</Window>

Remove the Marginand VerticalAlignmentproperties from these elements.

从这些元素中删除MarginVerticalAlignment属性。

Edit:Correct your XAML like this:

编辑:像这样更正您的 XAML:

<Window x:Class="Textpad.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Textpad" Height="324" Width="390" FontFamily="Tahoma">
    <DockPanel>

        <Menu Height="23" DockPanel.Dock="Top">
           <!-- MenuItems Here -->
        </Menu>

        <StatusBar Height="23" DockPanel.Dock="Bottom">
            <StatusBarItem Content="Amesinlola Tijesunimi is my Name and baseball is the game" />
        </StatusBar>

       <RichTextBox Margin="0,21" TextChanged="richTextBox1_TextChanged" VerticalContentAlignment="Top"/>

    </DockPanel>
</Window>