【问题标题】:How to achieve having 2 contorls on the same level in XAML layout in WPF?如何在 WPF 的 XAML 布局中实现同一级别的 2 个控件?
【发布时间】:2021-07-04 14:18:59
【问题描述】:

我正在尝试对标签、按钮和富文本框进行布局,以便标签和按钮位于文本框上方但位于不同的侧面。

我试过了:

    <ScrollViewer Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="6">
        <StackPanel>
            <DockPanel>
                <Label>some text</Label>
                <Button DockPanel.Dock="Right"  Width="30px">x</Button>
            </DockPanel>
            <RichTextBox Height="100px"></RichTextBox>
        </StackPanel>
    </ScrollViewer>

结果:

我也认为它可能会起作用:

    <ScrollViewer Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="6">
        <StackPanel>
            <StackPanel Orientation="Horizontal" FlowDirection="LeftToRight">
                <Label>some text</Label>
                <StackPanel Orientation="Horizontal" FlowDirection="RightToLeft">
                    <Button HorizontalAlignment="Right" Width="30px">x</Button>
                </StackPanel>
            </StackPanel>
            <RichTextBox Height="100px"></RichTextBox>
        </StackPanel>
    </ScrollViewer>

但它没有:

使用标签很容易,它是我想要的位置,但是我该怎么做才能将按钮放在文本框右上角的上方?我也见过 RenderTransform 属性,但我不确定这是否是一个好习惯。

【问题讨论】:

    标签: wpf layout


    【解决方案1】:

    试试这个:

     <ScrollViewer Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="6">
            <StackPanel>
                <Grid>
                    <RichTextBox Height="100px" ></RichTextBox>
                    <StackPanel Orientation="Horizontal" VerticalAlignment="Top">
                        <Label VerticalAlignment="Top">some text</Label>
                        <Button HorizontalAlignment="Right" Width="30px" VerticalAlignment="Top">x</Button>
                    </StackPanel>
                </Grid>
            </StackPanel>
        </ScrollViewer>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-03-09
      • 1970-01-01
      • 2016-04-27
      • 1970-01-01
      • 1970-01-01
      • 2018-11-15
      • 1970-01-01
      相关资源
      最近更新 更多