Wpf 一行中文本的左对齐和右对齐

Wpf 一行中文本的左对齐和右对齐,wpf,xaml,alignment,dockpanel,Wpf,Xaml,Alignment,Dockpanel,我想在一行中放置两个标签,第一个标签与左边框对齐,第二个标签与右边框对齐 就像这里: 以下是我的XAML尝试: <Window x:Class="MyTestNamespace.MyXAML" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

我想在一行中放置两个标签,第一个标签与左边框对齐,第二个标签与右边框对齐

就像这里:

以下是我的XAML尝试:

<Window x:Class="MyTestNamespace.MyXAML"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006">
    <DockPanel>
        <Label Content="left text" DockPanel.Dock="Left"></Label>
        <Label Content="right text" DockPanel.Dock="Right"></Label>
    </DockPanel>
</Window>

但我得到的却是:

  • 我对DockPanel做了什么错事
  • 如何实现第一张图片的设计(不一定使用DockPanel)

  • 您正确使用了dockpanel,但需要将标签内容向右对齐。试试这个

    <DockPanel>
            <Label Content="left text" DockPanel.Dock="Left"></Label>
            <Label Content="right text" DockPanel.Dock="Right" HorizontalContentAlignment="Right"></Label>
        </DockPanel>
    

    您正确使用了dockpanel,但需要将标签内容向右对齐。试试这个

    <DockPanel>
            <Label Content="left text" DockPanel.Dock="Left"></Label>
            <Label Content="right text" DockPanel.Dock="Right" HorizontalContentAlignment="Right"></Label>
        </DockPanel>