-->

Center content in combobox within datagridcombobox

2019-09-06 15:56发布

问题:

my first question, so please be gentle. I have tried for several days to figure it out. The closest I got was to programmatically access the content of the cell in the code behind. Is there a way to do it in the xaml?

For instance, this doesn't work

<Style x:Key="ComboBox" TargetType="DataGridCell">
    <Setter Property="FontSize" Value="14"/>
    <Setter Property="FontWeight" Value="SemiBold"/>
    <Setter Property="HorizontalAlignment" Value="Center"/>
    <Setter Property="HorizontalContentAlignment" Value="Center"/>
</Style>

<DataTemplate x:Key="foo">
    <Border Margin="3">
        <DataGrid Style="{StaticResource DGStyle}" Block.TextAlignment="Center">
           <DataGrid.Columns>
                <DataGridComboBoxColumn Header="bar" SelectedItemBinding="{Binding ListofIntegers, Mode=TwoWay}" 
                    ItemsSource="{Binding Source={StaticResource fooItem}, Path=ListofBar, Mode=OneWay}" CellStyle="{StaticResource ComboBox}" TextBlock.TextAlignment="Center"/>                    
                </DataGrid.Columns>
        </DataGrid>
    </Border>
</DataTemplate>

I have tried a few other things, having been able to center the headers, and textboxes in textbox columns. The comboboxes are not wanting to cooperate with me though. Any one know how?

Edit: Ok, feel stupid now, was playing around with Andy's solution and stumbled on this

<Style x:Key="BasicComboBox" TargetType="ComboBox">
        <Setter Property="HorizontalContentAlignment" Value="Center"/>
        <Setter Property="ItemContainerStyle">
            <Setter.Value>
                <Style TargetType="ComboBoxItem">
                    <!--can mess with appearance of drop down menu here-->
                </Style>
            </Setter.Value>
        </Setter>
    </Style>

<DataGridComboBoxColumn Header="Size" HeaderStyle="{StaticResource Header}"  SelectedItemBinding="{Binding Size.Name, Mode=TwoWay}" 
  ItemsSource="{Binding Source={StaticResource InsertSizes}}" EditingElementStyle="{StaticResource BasicComboBox}"/>

Can set the style of the generated combobox in EditingElementStyle.

Edit 2: While the above works, I think I'm going to go with Andy's suggestion of using a template column, the comboboxes don't blend in with the grid as well, but are much easier for the user to select, as there is no need to open editing mode.

回答1:

You could use a template column instead, I've used some junk data here but bindings work on the ComboBox in exactly the same way as they would from your DataGridComboBoxColumn.

<Window x:Class="WpfApplication4.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:sys="clr-namespace:System;assembly=mscorlib"
        Title="MainWindow" Height="350" Width="525">
    <DataGrid>
        <DataGrid.Items>
            <sys:String>a</sys:String>
        </DataGrid.Items>
        <DataGrid.Columns>
            <DataGridTemplateColumn Width="300">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <ComboBox HorizontalContentAlignment="Center">
                            <ComboBox.Items>
                                <sys:String>string1</sys:String>
                                <sys:String>string2</sys:String>
                                <sys:String>string3</sys:String>                                
                            </ComboBox.Items>
                        </ComboBox>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
        </DataGrid.Columns>
    </DataGrid>
</Window>