将按钮分成两半

发布于 2024-12-12 14:55:42 字数 2491 浏览 0 评论 0原文

我试图将一个按钮分成两半,以便可以在每一半中显示文本数据 - 当应用程序扩展时,文本数据的大小也可以增加。

这是我到目前为止得到的代码。

<Button Margin="16,0,16,6" Background="#daf0fc" BorderThickness="0" Height="Auto" HorizontalAlignment="Stretch" BorderBrush="White" Click="edit_house" Padding="0" Focusable="False">           

<Grid>

<Grid.RowDefinitions>
    <RowDefinition />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
    <ColumnDefinition />
    <ColumnDefinition />
</Grid.ColumnDefinitions>


                    <StackPanel Grid.Column="0" Orientation="Vertical" VerticalAlignment="Top" HorizontalAlignment="Left" Background="Black">
                        <StackPanel Orientation="Horizontal">
                            <TextBlock Margin="0,0,5,0" Text="{Binding house_number}" FontWeight="Bold" FontSize="14" />
                            <TextBlock Margin="0,0,0,0" Text="{Binding street}" FontWeight="Bold" FontSize="14" />
                        </StackPanel>
                        <TextBlock Text="{Binding postcode}" />
                        <TextBlock Margin="0,6,0,0" Text="{Binding house_type_text}" />
                    </StackPanel>

                    <StackPanel Grid.Column="1" Orientation="Vertical" VerticalAlignment="Top">
                        <StackPanel Orientation="Horizontal" VerticalAlignment="Top"  HorizontalAlignment="Left" Margin="0,2,0,0">
                            <TextBlock Text="£" HorizontalAlignment="Right" Foreground="#FF9D1818" FontWeight="ExtraBold" FontSize="16" />
                            <TextBlock Text="{Binding house_price}" HorizontalAlignment="Right" Foreground="#FF9D1818" FontWeight="ExtraBold" FontSize="16" />
                        </StackPanel>
                        <StackPanel Orientation="Horizontal" HorizontalAlignment="Left" Margin="0,0,0,0">
                            <TextBlock Margin="0,0,0,0" Text="{Binding sold_text}" HorizontalAlignment="Right" Foreground="#FF107910" FontWeight="ExtraBold" FontSize="14" />
                        </StackPanel>
                    </StackPanel>

我尝试将每个 StackPanel 一半设置为拉伸,但它们都只是浮动在按钮的中心,因此我无法对齐任一半上的文本。

这是我尝试创建的按钮的图形表示...

Button split in half

更新:

这是我的内容正在使用上面的代码,遗憾的是我正在努力让文本对齐,或者让任何东西拉伸到每一半的整个宽度:

I'm trying to split a button in half so that I can show text data in each half - which can also be increased in size when the application is expanded.

This is the code I've got soo far.

<Button Margin="16,0,16,6" Background="#daf0fc" BorderThickness="0" Height="Auto" HorizontalAlignment="Stretch" BorderBrush="White" Click="edit_house" Padding="0" Focusable="False">           

<Grid>

<Grid.RowDefinitions>
    <RowDefinition />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
    <ColumnDefinition />
    <ColumnDefinition />
</Grid.ColumnDefinitions>


                    <StackPanel Grid.Column="0" Orientation="Vertical" VerticalAlignment="Top" HorizontalAlignment="Left" Background="Black">
                        <StackPanel Orientation="Horizontal">
                            <TextBlock Margin="0,0,5,0" Text="{Binding house_number}" FontWeight="Bold" FontSize="14" />
                            <TextBlock Margin="0,0,0,0" Text="{Binding street}" FontWeight="Bold" FontSize="14" />
                        </StackPanel>
                        <TextBlock Text="{Binding postcode}" />
                        <TextBlock Margin="0,6,0,0" Text="{Binding house_type_text}" />
                    </StackPanel>

                    <StackPanel Grid.Column="1" Orientation="Vertical" VerticalAlignment="Top">
                        <StackPanel Orientation="Horizontal" VerticalAlignment="Top"  HorizontalAlignment="Left" Margin="0,2,0,0">
                            <TextBlock Text="£" HorizontalAlignment="Right" Foreground="#FF9D1818" FontWeight="ExtraBold" FontSize="16" />
                            <TextBlock Text="{Binding house_price}" HorizontalAlignment="Right" Foreground="#FF9D1818" FontWeight="ExtraBold" FontSize="16" />
                        </StackPanel>
                        <StackPanel Orientation="Horizontal" HorizontalAlignment="Left" Margin="0,0,0,0">
                            <TextBlock Margin="0,0,0,0" Text="{Binding sold_text}" HorizontalAlignment="Right" Foreground="#FF107910" FontWeight="ExtraBold" FontSize="14" />
                        </StackPanel>
                    </StackPanel>

I've tried setting each StackPanel half to stretch, but they both just Float in the centre of the button, so I can't align the text on either half.

Here's a graphical representation of the button I'm trying to create...

Button split in half

Update:

Here is what I'm getting with the code above, sadly I'm struggling to get the text to align, or to get anything to stretch to the full width of each half:

Update image

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

述情 2024-12-19 14:55:42

StackPanel 不会这样做;它只是堆叠元素。

相反,请使用具有两列的

StackPanel won't do that; it just stacks elements.

Instead, use a <Grid> with two columns.

ζ澈沫 2024-12-19 14:55:42

StackPanel 不是适合此操作的容器。尝试使用这样的网格:

<Button>
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition />
            <ColumnDefinition />
        </Grid.ColumnDefinitions>
        <StackPanel Grid.Column="0">
            ...
        </StackPanel>
        <StackPanel Grid.Colunn="1">
            ...
        </StackPanel>
    </Grid>
</Button>

StackPanel is not the right container for this. Try using a Grid like this:

<Button>
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition />
            <ColumnDefinition />
        </Grid.ColumnDefinitions>
        <StackPanel Grid.Column="0">
            ...
        </StackPanel>
        <StackPanel Grid.Colunn="1">
            ...
        </StackPanel>
    </Grid>
</Button>
白鸥掠海 2024-12-19 14:55:42

我喜欢使用 UniformGrids,尽管如果您需要单独定义行/列,网格也可以工作

,这是一个具有正确对齐方式的示例:

    <UniformGrid Columns="2">

        <StackPanel Background="LightBlue" >
            <StackPanel Orientation="Horizontal">
                <TextBlock Margin="0,0,5,0" Text="123" FontWeight="Bold" FontSize="14" />
                <TextBlock Margin="0,0,0,0" Text="Main Street" FontWeight="Bold" FontSize="14" />
            </StackPanel>
            <TextBlock Text="12345" />
            <TextBlock Margin="0,6,0,0" Text="Type" />
        </StackPanel>

        <StackPanel Background="Yellow">
            <StackPanel Orientation="Horizontal" VerticalAlignment="Top" Margin="0,2,0,0" HorizontalAlignment="Right">
                <TextBlock Text="£" HorizontalAlignment="Right" Foreground="#FF9D1818" FontWeight="ExtraBold" FontSize="16" />
                <TextBlock Text="100.00" HorizontalAlignment="Right" Foreground="#FF9D1818" FontWeight="ExtraBold" FontSize="16" />
            </StackPanel>
            <TextBlock Margin="0,0,0,0" Text="200.00" HorizontalAlignment="Right" Foreground="#FF107910" FontWeight="ExtraBold" FontSize="14" />
        </StackPanel>

    </UniformGrid>

您可能还有兴趣查看 此链接,它可以让您快速直观地了解 WPF 的每种不同布局及其行为方式。

I like to use UniformGrids, although a Grid will also work if you need to define your Rows/Columns individually

Here's an example, with correct alignments:

    <UniformGrid Columns="2">

        <StackPanel Background="LightBlue" >
            <StackPanel Orientation="Horizontal">
                <TextBlock Margin="0,0,5,0" Text="123" FontWeight="Bold" FontSize="14" />
                <TextBlock Margin="0,0,0,0" Text="Main Street" FontWeight="Bold" FontSize="14" />
            </StackPanel>
            <TextBlock Text="12345" />
            <TextBlock Margin="0,6,0,0" Text="Type" />
        </StackPanel>

        <StackPanel Background="Yellow">
            <StackPanel Orientation="Horizontal" VerticalAlignment="Top" Margin="0,2,0,0" HorizontalAlignment="Right">
                <TextBlock Text="£" HorizontalAlignment="Right" Foreground="#FF9D1818" FontWeight="ExtraBold" FontSize="16" />
                <TextBlock Text="100.00" HorizontalAlignment="Right" Foreground="#FF9D1818" FontWeight="ExtraBold" FontSize="16" />
            </StackPanel>
            <TextBlock Margin="0,0,0,0" Text="200.00" HorizontalAlignment="Right" Foreground="#FF107910" FontWeight="ExtraBold" FontSize="14" />
        </StackPanel>

    </UniformGrid>

You might also be interested in checking out this link, which gives you a quick visual look at each of WPF's different layouts and how they act.

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