Search code examples
c#.netwpfcomboboxright-align

WPF ComboBox: Align selected value and/or drop down items


I have an almost default WPF ComboBox:

enter image description here

What is the simplest way to align items, for instance, to the right in combobox's input and/or drop down?

I've looked around and found only solutions messing with the control templates which in my opinion is too long shot for such a simple thing. I can't believe there is no simpler solution we can do for aligning the items.

UPDATE: I've slightly reformulated the question to cover a broader set of cases further readers might experience on this topic as it turned out thanks to dkozl's answer.

Also, it should prevent some people from trying to close this question as a duplicate.


Solution

  • If you want to right align both selected value and drop down items then setting HorizontalContentAlignment="Right" against ComboBox should be enough

    <ComboBox ... HorizontalContentAlignment="Right">
    

    if you want to right align only drop down items then you need to change HorizontalContentAlignment of the ComboBoxItem

    <ComboBox>
        <ComboBox.ItemContainerStyle>
            <Style TargetType="{x:Type ComboBoxItem}">
                <Setter Property="HorizontalContentAlignment" Value="Right"/>
            </Style>
        </ComboBox.ItemContainerStyle>
    </ComboBox>
    

    and to right align only selected value combination of both

    <ComboBox ... HorizontalContentAlignment="Right">
        <ComboBox.ItemContainerStyle>
            <Style TargetType="{x:Type ComboBoxItem}">
                <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
            </Style>
        </ComboBox.ItemContainerStyle>
    </ComboBox>