How to make cell to take full width when aligned to right or left in WPF grid

梦想与她 提交于 2019-12-24 04:38:13

问题


I'm designing some grid in WPF and want to display numbers aligned to right, but when I set HorizontalAlignment=Right the cell itself don't use all available width, so the border is painted half based on content. Look at the attached picture.

The code is:

<Grid Width="620" Name="tblTaxBalance">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="110"/>
        <ColumnDefinition Width="350"/>
        <ColumnDefinition Width="80" Style="{StaticResource CellRightAlign}"/>
        <ColumnDefinition Width="80" Style="{StaticResource CellRightAlign}"/>
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="Auto"/>
    </Grid.RowDefinitions>

    <!-- row 1 -->
    <Label Grid.RowSpan="2" Grid.Row="0" Style="{StaticResource TaxTableCellStyle}" BorderThickness="1">Тайлангийн төрөл</Label>
    <Label Grid.RowSpan="2" Grid.Row="0" Grid.Column="1" Style="{StaticResource TaxTableCellStyle}" BorderThickness="0,1,1,1">Татварын төрөл</Label>
    <Label Grid.ColumnSpan="2" Grid.Row="0" Grid.Column="2" Style="{StaticResource TaxTableCellStyle}" BorderThickness="0,1,1,1">Эцсийн үлдэгдэл /мян. төг/</Label>

    <!-- row 2 -->
    <Label Grid.RowSpan="2" Grid.Row="1" Grid.Column="2" Style="{StaticResource TaxTableCellStyle}" BorderThickness="0,0,1,1" HorizontalAlignment="Right">Дутуу</Label>
    <Label Grid.RowSpan="2" Grid.Row="1" Grid.Column="3" Style="{StaticResource TaxTableCellStyle}" BorderThickness="0,0,1,1">Илүү</Label>
</Grid>

回答1:


Use HorizontalAlignment Stretch and set FlowDirection RightToLeft

Like that:

<Label Grid.RowSpan="2" Grid.Row="1" Grid.Column="2" Style="{StaticResource TaxTableCellStyle}" BorderThickness="0,0,1,1" HorizontalAlignment="Stretch" FlowDirection="RightToLeft">Дутуу</Label>


来源:https://stackoverflow.com/questions/18378515/how-to-make-cell-to-take-full-width-when-aligned-to-right-or-left-in-wpf-grid

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!