0

所以我在网格列的窗口上有一个按钮。该按钮如下图所示:

<Button>
    <Border BorderThickness="3" BorderBrush="Red">
        <Grid Width="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=Button, AncestorLevel=1}, Path=Width}">
            <Grid.RowDefinitions>
                <RowDefinition/>
                <RowDefinition/>
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>
                <TextBlock Grid.Row="0" Background="LightBlue">bunnies</TextBlock>
            <TextBlock Grid.Row="1" Width="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=Button, AncestorLevel=1}, Path=Width}" Background="LightBlue">bunnies</TextBlock>
        </Grid>
    </Border>
</Button>

我看到的是下面最左边按钮中的示例,我希望它看起来像下面的 2。

编辑:如果您将 Width="1000" 添加到第一个 TextBlock,您将看到我想要的效果,除了文本不再是列的中心,而是 1000 宽的块的中心。所以我所追求的是实际列宽或实际按钮宽度的绑定语法。

我留下了一些我尝试过的东西,改变了单列定义的宽度,绑定的东西,等等。

左 = 1 右 = 2

忽略对文本的狡猾编辑,我只希望边框到达按钮的边缘。谢谢

4

1 回答 1

2

试试这个:

<Button HorizontalContentAlignment="Stretch">
            <Border BorderThickness="3" BorderBrush="Red">
                <Grid HorizontalAlignment="Stretch">
                    <Grid.RowDefinitions>
                        <RowDefinition/>
                        <RowDefinition/>
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*"/>
                    </Grid.ColumnDefinitions>
                    <TextBlock Grid.Row="0" TextAlignment="Center" Background="LightBlue">bunnies</TextBlock>
                    <TextBlock Grid.Row="1" TextAlignment="Center" Background="LightBlue">bunnies</TextBlock>
                </Grid>
            </Border>
        </Button>
于 2012-10-16T19:35:31.783 回答