如何获取按钮内容以填充 WPF 中的宽度 [英] How to get button content to fill width in WPF

查看:32
本文介绍了如何获取按钮内容以填充 WPF 中的宽度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的 XAML 中,按钮将拉伸以适应窗口的宽度,包括调整窗口大小时.但是,TextBlock 和蓝色框居中.你会如何改变它,以便:1) TextBlock 在 Button 内,但与实际 Button 宽度左对齐(即在窗口的左侧)2)画布在按钮内,但与实际按钮宽度右对齐(即在窗口的右侧)

In the following XAML the button will stretch to fit the width of the window, including as you resize the window. However, the TextBlock and blue box are centered. How would you change it so that: 1) the TextBlock is inside the Button, but left justified with the actual Button width (i.e. on the left side of the Window) 2) the Canvas is inside the Button, but right-justified with the actual Button width (i.e. on the right side of the Window)

在这种情况下,Horizo​​ntalAlignment=Stretch"似乎不起作用,并且在使用自动调整大小时,按钮内的网格只会增长到其内容所需的最小宽度.

It seems "HorizontalAlignment=Stretch" doesn't work in this case, and, when using Auto sizing, the Grid inside the Button only ever grows to the minimum width needed for its contents.

<Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Name="Window"
    Title="test"
    Width="640" Height="480">

    <Grid x:Name="LayoutRoot">
        <Button Height="30">
            <Grid HorizontalAlignment="Stretch">
                <TextBlock Text="Sample Text" HorizontalAlignment="Stretch" TextAlignment="Left"></TextBlock>
                <Canvas Width="40" Background="AliceBlue" HorizontalAlignment="Right"></Canvas>
            </Grid>
        </Button>
    </Grid>
</Window>

推荐答案

你应该设置 Button.Template.也可以使用 Grid.ColumnDefinitions 来正确设置内部元素的位置和宽度.

You should set Button.Template. Also Grid.ColumnDefinitions can be used to set the position and width of elements inside properly.

        <Button Height="30" Content="Smaple text">
            <Button.Template>
                <ControlTemplate TargetType="{x:Type Button}">
                    <Grid>
                        <Border Background="{TemplateBinding Background}"
                         BorderBrush="{TemplateBinding BorderBrush}"
                         BorderThickness="{TemplateBinding BorderThickness}">
                            <Grid>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="Auto"/>
                                    <ColumnDefinition Width="*"/>
                                </Grid.ColumnDefinitions>
                                <ContentPresenter HorizontalAlignment="Left"  Grid.Column="0"
                                          VerticalAlignment="Center"/>
                                <Canvas Background="AliceBlue" Grid.Column="1" />
                            </Grid>
                        </Border>
                    </Grid>
                </ControlTemplate>
            </Button.Template>
        </Button>

这篇关于如何获取按钮内容以填充 WPF 中的宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆