简体   繁体   English

Horizo​​ntalAlignment在DataGridTextColumn中的ComboBox上不起作用

[英]HorizontalAlignment not working on ComboBox in DataGridTextColumn

I have a DataGridTextColumn whose header consists of a TextBlock and a ComboBox . 我有一个DataGridTextColumn其标题由TextBlockComboBox 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以及包含TextBlockComboBoxGrid 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.

相关问题 HorizontalAlignment="Stretch" 在 TreeViewItem 中不起作用 - HorizontalAlignment="Stretch" not working in TreeViewItem 图像上的Horizo​​ntalAlignment在itextsharp中的PdfPCell中不起作用 - HorizontalAlignment on image not working in PdfPCell in itextsharp MultiBinding在DataGridTextColumn中不起作用 - MultiBinding not working in DataGridTextColumn IMul​​tiValueConverter的DataGridTextColumn绑定不起作用 - DataGridTextColumn binding for IMultiValueConverter not working 如何以编程方式在 Datatgrid WPF 中的 DataGridTextColumn 中绑定组合框 - How to Binding Combobox in DataGridTextColumn in Datatgrid WPF Programmatically DataGridTextColumn中的组合框不占据其父级Width - Combobox inside DataGridTextColumn not occupying it's parent Width 参数为hibernateConnector的DataGridTextColumn ValidationRule不起作用 - DataGridTextColumn ValidationRule with parameter hibernateConnector not working 如何将带有值转换器的WPF组合框所选项目绑定到DataGridTextColumn? DataGridTextColumn和combobox都是datagrid列 - How to bind WPF combobox selected item with a Value Converter to a DataGridTextColumn? Both DataGridTextColumn and combobox are datagrid columns MVVM; 标题中带有ComboBox的DataGridTextColumn,它确定列的绑定 - MVVM; DataGridTextColumn with a ComboBox in the header, which determines the Binding of the Column WPF DataGridTextColumn FontWeight 绑定/值转换器不起作用 - WPF DataGridTextColumn FontWeight Binding / Value Converter not working
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM