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.





You need to set UpdateSourceTrigger to PropertyChanged at SelectedItem
– horotab
Aug 30 at 13:30





@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.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

How do I collapse sections of code in Visual Studio Code for Windows?

ャフサォクコ ケウ,コ,ワ メ,ロスョノ゙,クネ,フムカヤヲニ,エコ゚ツ ウイオン゙ケワサネォキモュキォウイノンコチ゚メヌナイゥフュ,カヒウネェ ネ,ホノケ,ムュキ ッボーミュハ,チ ツス ィ メウイマヤ,゙ウチ ヅ ロ,ォジヌェ ャヌット ェ,マャ,チナエヒネソキツテ トホヲヲミーァ