It turns out, that the content of the ComboBoxItem
filled the entire space, only when an event SelectionChanged
triggered.
Example:
XAML
<ComboBox Width="300"
Height="30"
SelectionChanged="ComboBox_SelectionChanged">
<ComboBoxItem>Test</ComboBoxItem>
<ComboBoxItem Name="comboBoxItem"
HorizontalContentAlignment="Stretch"
Width="Auto">
<DockPanel Background="Red"
HorizontalAlignment="Stretch"
Width="Auto">
...
</DockPanel>
</ComboBoxItem>
</ComboBox>
Code-behind
private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
MessageBox.Show(comboBoxItem.ActualWidth.ToString());
}
When you start the application ActualWidth
of ComboBoxItem is zero, however when SelectionChanged
event triggered value will be 298.
Workaround
For workaround add ComboBoxItem
in begin, for example: Select item
and set for ComboBox SelectedIndex="0"
like this:
<ComboBox Width="300"
Height="30"
SelectedIndex="0">
<ComboBoxItem>Select item</ComboBoxItem>
...