469,000 Members | 1,795 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,000 developers. It's quick & easy.

WPF ComboBox SelectedItem binding sometimes not working properly

2
Hello, i am using WPF combobox in my application.
1) I create and add a combobox to a visual container (usualy Grid) by runtime or it is defined in the XAML page
2) I assign combobox source to the new xmldocument, this loads the content and then document xml data populate the combo items
3) When page init is finished i load page data to setup the form, I set loaded comboboxes to the selecteditems
4) page.show();

this seems to work, but there is like 10% chance that the combobox is loaded, but the selecteditem is blank although the itemsource was populated and selecteditem was set to proper id.
When i trace the code in debug it works on 100% but when i run it without debug it occasionaly forgets to setup the combobox selection.

I have found that this is some sort of bug of WPF combobox, and its possible to "correct" it in the XAML by swaping:
Expand|Select|Wrap|Line Numbers
  1. <ComboBox ItemsSource="{Binding Countries, Mode=OneWay}" SelectedItem="{Binding SelectedCountry}" DisplayMemberPath="Name" > </ComboBox>
  2.  
  3. to:
  4.  
  5. <ComboBox SelectedItem="{Binding SelectedCountry}" ItemsSource="{Binding Countries, Mode=OneWay}" DisplayMemberPath="Name" > </ComboBox>
  6.  
but because i setup and load data in runtime, i do the bindings in the runtime without XAML. Is there any workarround for this?

is there any event or trigger that can help to find out, if the combobox has realy finished loading? Also some switch that will forbid combobox to load itemsource in separate thread would be good, as it is unstable in current combobox version.I am using .Net 3.5 SP1

I have tried also this:

SelectedValueId is String property that buffers the id of target record ...
Expand|Select|Wrap|Line Numbers
  1. combo1.Loaded += delegate { if (SelectedValueId != null) SetSelected(); };
  2.  
but its still buggy, sometimes the combobox doesnt select choosen item properly although it is supposed to do it correctly from the inputs and in debug
May 12 '10 #1
1 9417
kimic
2
So probably i have found the cause of the problem, the combobox was correct, but the source was xmlprovider, which was running asynchronous by the default.

this code fixed it:



xmlsourcedata.IsAsynchronous=false;

xmlsourcedata.IsInitialLoadEnabled = true;

xmlsourcedata.Document.InnerXml = trn2.GetRootNode().OuterXml;

xmlsourcedata.Refresh();



...

Combobox binding code

..

Combobox SetSelectedId code
May 12 '10 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

3 posts views Thread by ScottO | last post: by
5 posts views Thread by Nita Raju | last post: by
3 posts views Thread by Simon Tamman | last post: by
5 posts views Thread by damezumari | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by gregeryvm | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.