ComboBox Binding Confusoin

R

ryan baldwin

I have a WPF combobox in which I'm binding the SelectedIndex to a model
class I've developed. Assume something similar to the following (but
with different names, etc... this is a simplified version):

<ComboBox Name="myCombo" ItemsSource="{Binding ComboSource}"
SelectedIndex="{Binding IndexOfCombo, Mode=OneWay}" />

In my model I occasionally will set IndexOfCombo back to 0, and will
then publish a NotifyPropertyChanged for IndexOfCombo. This sets the
selected index of the combobox to 0 as I would expect.... UNLESS the
user has already selected something int he combobox. For whatever
reason, it's as though the combobox will ignore the
NotifyPropertyChanged if the user has selected an item in the combobox.

Is this behaviour expected? Is there a way to prohibit the combobox
from ignoring the NotifyPropertyChanged event? I'm very confused as to
why this is happening.

Any help would be gracefully accepted, as this issue has burned my
entire morning...

Thanks!

- ryan.
 
Z

Zhi-Xin Ye [MSFT]

Dear ryan,

I think this is a by design feature, when changes the selection of the
ComboBox manually, the value of the SelectedIndex property is changed,
naturally you would expect to retrieve the value later, but if the binding
remained, it could be updated before you had a chance to retrieve that
value, if you're developing a multithreaded system, this would cause
undesired effects.

So the system tends to discard the binding when the target property is
manually changed. You can check this feature by checking the return value
of the ComboBox.GetBindingExpression() method, if you don't manually change
the selection of the ComboBox, this method returns the BindingExpression
that you're currently used, but if you manually change the selection of the
ComboBox, this method returns null, which means the binding is discarded.

If you want the binding continue to work after manually changing the
selection, you can reset the binding, something like this:

private void button1_Click(object sender, RoutedEventArgs e)
{
if
(this.comboBox1.GetBindingExpression(ComboBox.SelectedIndexProperty) ==
null)
{
this.comboBox1.SetBinding(ComboBox.SelectedIndexProperty,
binding);
}
m.IndexOfCombo = 1;
}

If any of my explanation is unclear, please let me know.

Sincerely,
Zhi-Xin Ye
Microsoft Managed Newsgroup Support Team

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://support.microsoft.com/select/default.aspx?target=assistance&ln=en-us.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top