Reputation: 11
I have a Combobox in a WPF application containing three items, that allows a user to switch languages.
<ComboBox x:Name="cmbSelectLanguage" Margin="81,53,0,0" SelectionChanged="cmbSelectLanguage_SelectionChanged" TabIndex="1" HorizontalAlignment="Left" Width="150" Height="30" VerticalAlignment="Top" SelectedIndex="0">
<ComboBoxItem Name="enUS">
<TextBlock Text="English - US"/>
</ComboBoxItem>
<ComboBoxItem Name="enGB">
<TextBlock Text="English - UK"/>
</ComboBoxItem>
<ComboBoxItem Name="elGR">
<TextBlock Text="Greek"/>
</ComboBoxItem>
</ComboBox>
The problem is, that when the window initially loads, it won't display the default value. It shows nothing, like this:
As soon as I move the mouse over the Combobox or over the button to the bottom right, then the default item appears, like this:
I tried to set the Combobox SelectedIndex through both XAML and the code-behind after the Window loads, but nothing seems to work.
What could possibly cause this behavior and how would I go about fixing it?
EDIT: Thanks for your responses. I found the solution by chance. After removing lots of XAML and some code-behind code I noticed that the problem was gone after deleting this from my Window XAML definition: SizeToContent="WidthAndHeight"
All my other code is exactly the same, this is the only thing I had to change after all. Even though I don't have a clue as to why, it works :)
Upvotes: 0
Views: 3862
Reputation: 163
Your problem solved this way, but there is an other way that I used when I stuck in such problem and it works just fine for me.
comboBox has a load event just like window that you can use it. simply go to it's event list and choose load then set selection. Remember to chose the select index in xaml or in the windows load first. It's useful when you bind your combo Box to a DB and in the window load page set conditionally to some index and repeat it in load event so the result is you see your selection in window load and do not need to click the combo box to see visual update of it's selection change.
See the example code below. Remember you can chose the selected index programmatically or conditionally base on your need int windows load time instead of xaml (as I do in this example). it's all depend to you.
<ComboBox
x:Name="comboItemList"
Height="22"
With="80"
VerticalAlignment="Center"
HorizontalAlignment="Center"
HorizontalContentAlignment="Center"
VerticalContentAlignment="Center"
Loaded="OnComboBoxLoad"
SelectedIndex="0">
<TextBlock Text="Item01" TextAlignment="Center" />
<TextBlock Text="Item02" TextAlignment="Center" />
<TextBlock Text="Item03" TextAlignment="Center" />
</ComboBox>
Now in code behind you should use the code below or similar to it as your condition (below code is 100% tested)
private void OnComboBoxLoad(object sender, RoutedEventArgs e)
{
//store current selcted index in variable
int tempIndex = ((ComboBox)sender).SelectedIndex;
//// ... Make the your desire item selected.
((ComboBox)sender).SelectedIndex = -1;
((ComboBox)sender).SelectedIndex = tempIndex ;
}
Upvotes: 0
Reputation: 4631
As already mentioned, the problem lies outside this code. If I create a new WPF project, and paste the below code into the MainWindow's Grid, it shows up fine:
<Window x:Class="WpfApplication6.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<ComboBox x:Name="cmbSelectLanguage" Margin="81,53,0,0" TabIndex="1" HorizontalAlignment="Left" Width="150" Height="30" VerticalAlignment="Top" SelectedIndex="0">
<ComboBoxItem Name="enUS">
<TextBlock Text="English - US"/>
</ComboBoxItem>
<ComboBoxItem Name="enGB">
<TextBlock Text="English - UK"/>
</ComboBoxItem>
<ComboBoxItem Name="elGR">
<TextBlock Text="Greek"/>
</ComboBoxItem>
</ComboBox>
</Grid>
Try commenting out the SelectionChanged event and see if it works.
Upvotes: 1