如何在网格中设置列的右边框?

时间:2014-01-20 19:53:06

标签: wpf xaml

我有一个像这样的网格:

<Grid>     
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="1*"/>
            <ColumnDefinition Width="4*"/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="1*"/>
        </Grid.RowDefinitions>


        <TextBlock Grid.Row="0" Grid.Column="0">Row 0, Column 1</TextBlock>
        <TextBlock Grid.Row="1" Grid.Column="0">Row 1, Column 1</TextBlock>
        <TextBlock Grid.Row="2" Grid.Column="0">Row 2, Column 1</TextBlock>
        <TextBlock Grid.Row="3" Grid.Column="0">Row 3, Column 1</TextBlock>

        <TextBlock Grid.Row="0" Grid.Column="1" Grid.RowSpan="4">Column 1</TextBlock>
    </Grid>

我不会在实际应用程序中实际使用TexBlocks,我用它们来使示例更容易。 基本上我想设置一个边框,它将第0列中的所有内容从第1列中删除。

你是怎么做到的?

2 个答案:

答案 0 :(得分:11)

显示Border的最佳方法之一是使用Border元素。你可以在后面声明其他内容:

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="1*"/>
        <ColumnDefinition Width="4*"/>
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="1*"/>
    </Grid.RowDefinitions>

    <Border Grid.Column="1" Grid.RowSpan="4" BorderBrush="Black" 
        BorderThickness="1,0,0,0" Background="{x:Null}" />

    <TextBlock Grid.Row="0" Grid.Column="0">Row 0, Column 1</TextBlock>
    <TextBlock Grid.Row="1" Grid.Column="0">Row 1, Column 1</TextBlock>
    <TextBlock Grid.Row="2" Grid.Column="0">Row 2, Column 1</TextBlock>
    <TextBlock Grid.Row="3" Grid.Column="0">Row 3, Column 1</TextBlock>
    <TextBlock Grid.Row="0" Grid.Column="1" Grid.RowSpan="4">Column 1</TextBlock>
</Grid>

答案 1 :(得分:3)

有许多方法可以实现它,最简单,只需画一条线(除了我被骗并使用了一个形状......我知道,我很可怕吗?但是你明白了......)希望这个帮助

<Grid>     
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="1*"/>
            <ColumnDefinition Width="4*"/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="1*"/>
        </Grid.RowDefinitions>

        <Rectangle Grid.RowSpan="4" Width="1" Fill="Red" HorizontalAlignment="Right"/>

        <TextBlock Grid.Row="0" Grid.Column="0">Row 0, Column 1</TextBlock>
        <TextBlock Grid.Row="1" Grid.Column="0">Row 1, Column 1</TextBlock>
        <TextBlock Grid.Row="2" Grid.Column="0">Row 2, Column 1</TextBlock>
        <TextBlock Grid.Row="3" Grid.Column="0">Row 3, Column 1</TextBlock>

        <TextBlock Grid.Row="0" Grid.Column="1" Grid.RowSpan="4">Column 1</TextBlock>
    </Grid>