[英]HorizontalAlignment not working on ComboBox in DataGridTextColumn
I have a DataGridTextColumn
whose header consists of a TextBlock
and a ComboBox
. 我有一个DataGridTextColumn
其标题由TextBlock
和ComboBox
。 I would like the TextBlock
to be left aligned and the ComboBox
to be right-aligned. 我希望TextBlock
保持左对齐,而ComboBox
保持右对齐。 When I try setting that on ComboBox
using HorizontalAlignment = "Right"
, it does not work! 当我尝试使用HorizontalAlignment = "Right"
在ComboBox
进行设置时,它不起作用! For the header I tried with StackPanel
as well as a Grid
for containing the TextBlock
and ComboBox
. 对于标题,我尝试使用StackPanel
以及包含TextBlock
和ComboBox
的Grid
。 What I am doing wrong? 我做错了什么?
<DataGrid
CanUserSortColumns="True"
IsReadOnly="True">
<DataGrid.Columns>
<DataGridTextColumn Header="Time"/>
<DataGridTextColumn>
<DataGridTextColumn.Header>
<StackPanel Orientation="Horizontal">
<TextBlock Text="Money" VerticalAlignment="Center"/>
<ComboBox HorizontalAlignment="Right" VerticalAlignment="Center"
Width="100" ItemsSource="{Binding comboBoxItems}" SelectedIndex="0">
</ComboBox>
</StackPanel>
</DataGridTextColumn.Header>
</DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
Attempt with Grid: 尝试使用网格:
<DataGridTextColumn.Header>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0" Text="Money" VerticalAlignment="Center"/>
<ComboBox Grid.Column="1" HorizontalAlignment="Right" VerticalAlignment="Center"
ItemsSource="{Binding comboBoxItems}" SelectedIndex="0">
</ComboBox>
</Grid>
</DataGridTextColumn.Header>
EDIT 1: I would like the ComboBox to be stretched if the column is dragged to increase the column width. 编辑1:如果将列拖动以增加列宽,我希望ComboBox被拉伸。 Otherwise atleast the ComboBox should align to the right if the column is dragged on the right side. 否则,如果将列拖动到右侧,则ComboBox至少应与右侧对齐。
EDIT 2: The important thing here is that when I drag the column to increase its width, the ComboBox should fall on the right side. 编辑2:这里重要的是,当我拖动列以增加其宽度时,ComboBox应该位于右侧。
use this 用这个
<DataGridTextColumn Width="120">
<DataGridTextColumn.HeaderTemplate>
<DataTemplate>
<Grid Width="{Binding Path=ActualWidth, RelativeSource={RelativeSource AncestorType={x:Type DataGridColumnHeader}}}">
<TextBlock Text="LLLLL" HorizontalAlignment="Left" Margin="5"/>
<TextBlock Text="RRRRR" HorizontalAlignment="Right" Margin="5"/>
</Grid>
</DataTemplate>
</DataGridTextColumn.HeaderTemplate>
</DataGridTextColumn>
Try DockPanel
instead of StackPanel
. 尝试使用DockPanel
而不是StackPanel
。
<DockPanel LastChildFill="True">
<TextBlock Text="Money" VerticalAlignment="Center" DockPanel.Dock="Left"/>
<ComboBox VerticalAlignment="Center" SelectedIndex="0"/>
</DockPanel>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.