C# ComboBox SelectedItem not Updating
C# ComboBox SelectedItem not Updating
My problem is that after I select a item in the ComboBox, the first item or "default" item of the combobox stays empty but if I click the combobox the values beneath show up are selectable etc. but I want the clicked one to show in the "default/first" place.
What I tried so far
XAML:
<ComboBox Margin="55,0,0,10" Height="20" Width="145" VerticalAlignment="Center" HorizontalAlignment="Left"
ItemsSource="Binding TabItems, Source=StaticResource MainWindowViewModelRefactored, Mode=TwoWay"
SelectedItem="Binding SelectedItem, Source=StaticResource MainWindowViewModelRefactored, Mode=TwoWay"
DisplayMemberPath="Header">
</ComboBox>
Property:
public TabItem SelectedItem
get
return _selectedItem;
set
UpdateTCVCollection(value);
_selectedItem = value;
NotifyPropertyChanged("SelectedItem");
If I open up the combobox the selecteditem is highlighted, but I also want it to be shown in the "first place" when the ComboBox is closed.
@horotab thanks, tried that didnt work out.
– C.User
Aug 30 at 13:32
This should work by default? How does your ComboBox look like? Is TabItem a custom type of yours? What type is TabItems and what does UpdateTCVCollection do? Your issue is not reproducible based on the information you have provided.
– mm8
Aug 31 at 14:22
2 Answers
2
You can add a method when the index has changed, then remove the item that the user selected and add it at the begining.
I've set the value of Sorted to false because that way the value you selected won't be reorganized in your ComboBox.
Sorted
false
private void ComboBox1_SelectedIndexChanged(object sender, EventArgs e)
RadItem selectedItem = ComboBox1.SelectedItem as RadItem;
if (selectedItem != null)
ComboBox1.Items.Remove(selectedItem);
ComboBox1.Items.Sorted = true;
ComboBox1.Items.Sorted = false;
ComboBox1.Items.Insert(0, selectedItem);
ComboBox1.Text = selectedItem.Text;
Add the UpdateSourceTrigger to your Combobox.
UpdateSourceTrigger=PropertyChanged
Example:
<ComboBox Margin="55,0,0,10" Height="20" Width="145" VerticalAlignment="Center" HorizontalAlignment="Left"
ItemsSource="Binding TabItems, Source=StaticResource MainWindowViewModelRefactored, Mode=TwoWay"
SelectedItem="Binding SelectedItem, Source=StaticResource MainWindowViewModelRefactored, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged"
DisplayMemberPath="Header">
</ComboBox>
Have a look to this MSDN link
That should help you with your problem.
Greetings
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
You need to set UpdateSourceTrigger to PropertyChanged at SelectedItem
– horotab
Aug 30 at 13:30