0
votes

I try to do a template for a combobox. The ItemTemplate is ok but not the selected item as u can see below:

enter image description here

My code:

                <ComboBox ItemsSource="{Binding CouleursList}" 
                      SelectedItem="{Binding SelectedCouleur}" 
                      Grid.Column="1" Grid.Row="2">
                <ComboBox.ItemContainerStyle>
                    <Style TargetType="{x:Type ComboBoxItem}">
                        <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
                    </Style>
                </ComboBox.ItemContainerStyle>
                <ComboBox.ItemTemplate>
                    <DataTemplate>
                        <Grid>
                            <Rectangle Stroke="Black" Margin="1" Height="15"
                                       HorizontalAlignment="Stretch">
                                <Rectangle.Fill>
                                    <SolidColorBrush Color="{Binding Path=., Converter={StaticResource ColorConverter}}"/>
                                </Rectangle.Fill>
                            </Rectangle>
                        </Grid>
                    </DataTemplate>
                </ComboBox.ItemTemplate>
            </ComboBox>

If I gave a fixed width, that works but the rectangle is align to the left and center for the list

Thanks !

1

1 Answers

0
votes

The selected item content isn't displayed in a ComboBoxItem, so your HorizontalContentAlignment style setter won't apply.

You can set that property on the ComboBox itself, however:

<ComboBox 
    ItemsSource="{Binding CouleursList}" 
    SelectedItem="{Binding SelectedCouleur}"
    HorizontalContentAlignment="Stretch"