c# - 如何使单元格在 WPF 网格中向右或向左对齐时采用全宽度

标签 c# wpf datagrid grid-layout

我正在 WPF 中设计一些网格,想要显示右对齐的数字,但是当我设置 Horizo​​ntalAlignment=Right 时,单元格本身不使用所有可用宽度,因此边框根据内容绘制一半。请看附图。

enter image description here

代码是:

<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>

最佳答案

使用 Horizo​​ntalAlignment Stretch 并设置 FlowDirection RightToLeft

像这样:

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

关于c# - 如何使单元格在 WPF 网格中向右或向左对齐时采用全宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18378515/

相关文章:

forms - 如何在奏鸣曲管理包中将 NULL 作为数据网格关系中的选项?

c# - 如何在 Roslyn 中为方法创建没有 ref 或 out 的命名参数?

c# - EF5 逆属性问题

c# - WPF - 如何在 ViewModel 之间共享集合的单个实例?

c# - CefSharp 3 和 SetZoomLevel

wpf - 在 WPF 网格/列表中跨越多列的最佳方法?

wpf - 将Click事件添加到PowerShell中的WPF DataGrid按钮列中

c# - 动态自定义树访问

c# - 如何在本地测试 Azure Functions 身份验证

wpf - Rx DragBehavior->为什么我的元素不动