Horizo​​ntalAlignment无法在DataGridTextColumn中的ComboBox上运行

时间:2015-01-14 15:43:29

标签: c# wpf datagrid

我有一个DataGridTextColumn,其标题包含TextBlockComboBox。我希望TextBlock左对齐,ComboBox右对齐。当我尝试使用ComboBoxHorizontalAlignment = "Right"上进行设置时,它不起作用!对于我使用StackPanel尝试的标头以及Grid包含TextBlockComboBox的标头。我做错了什么?

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

尝试使用网格:

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

编辑1:如果拖动列以增加列宽,我希望拉伸ComboBox。否则,如果在右侧拖动列,则至少ComboBox应该向右对齐。

编辑2:重要的是,当我拖动列以增加其宽度时,ComboBox应该落在右侧。

2 个答案:

答案 0 :(得分:1)

使用此

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

答案 1 :(得分:0)

尝试使用DockPanel代替StackPanel

<DockPanel LastChildFill="True">
        <TextBlock Text="Money" VerticalAlignment="Center" DockPanel.Dock="Left"/>
        <ComboBox  VerticalAlignment="Center" SelectedIndex="0"/>
</DockPanel>