ComboBoxItem for full width ComboBox when using SelectedIndex or a navigation keyboard?

Following XAML, โ€œMy stuffโ€ is in the center ComboBoxuntil I open ComboBoxwhen it stretches correctly.

    <ComboBox Height="30" Width="300" HorizontalContentAlignment="Stretch" SelectedIndex="0">
        <ComboBoxItem HorizontalContentAlignment="Stretch">
            <Border Background="Red">
                <TextBlock>My stuff...</TextBlock>
            </Border>
        </ComboBoxItem>
    </ComboBox>

The question is, is it possible to stretch ComboBoxItemeven if it was selected with SelectedIndex? The same error or function occurs if SelectedIndex is untouched (-1), and one selects the item using the keyboard.

The workaround is probably to launch ComboBox programmatically at the beginning of the application, which is pretty ugly.

+3
source share
2 answers

:

.

Width="{Binding ElementName=combox1, Path=ActualWidth}">

:

<ComboBox x:Name="combox1" Height="30" Width="300" HorizontalContentAlignment="Stretch" 
    SelectedIndex="0">
    <ComboBoxItem HorizontalContentAlignment="Stretch">
        <Border Background="Red" Width="{Binding ElementName=combox1, Path=ActualWidth}">
            <TextBlock>My stuff...</TextBlock>
        </Border>
    </ComboBoxItem>
</ComboBox>
+6

, - , . , . , , , .

, - , , , , , , .

:

    <ComboBox Background="Red" x:Name="combox2" Height="30" HorizontalContentAlignment="Stretch" SelectedIndex="0">
        <ComboBoxItem Background="Red" HorizontalContentAlignment="Stretch">
            <TextBlock Background="Red">My stuff...</TextBlock>
        </ComboBoxItem>
    </ComboBox>

, !:)

YourCodeFactory.com

+1

Source: https://habr.com/ru/post/1720365/


All Articles