开发者

How can I in Silverlight determine if it is the user that changed a ComboBox?

In the ComboBox.SelectionChanged event I want to know who changed the ComboBox value, the user or some other code.

At first I checked if cmbBox.IsDropDownOpen is true. But this is false if the user changed the ComboBox with the keyboard.

Then I thought to check if it focused. Silverlight ComboBox doesn't have a IsFocused property so I saved the state in the cmbBoxGotFocus and cmbBoxLostFocus event handlers:

开发者_如何转开发
    private bool cmbBox_isFocused = false;
    private void cmbBox_GotFocus(object sender, RoutedEventArgs e)
    {
        cmbSalesPerson_isFocused = true;
    }

    private void cmbBox_LostFocus(object sender, RoutedEventArgs e)
    {
        cmbSalesPerson_isFocused = false;
    }

Unfortunately this doesn't work either because there is some strange behaviour I don't understand: when the user clicks on the ComboBox the 2 events are fired one after the other so the bool remains false.


If you're using the Model-View-ViewModel pattern (which is the de-facto standard for WPF and Silverlight apps, and for good reason), then this should be trivial: when the combobox's value is changed, Silverlight will set your ViewModel's property automatically. You can put logic in your property setter to react to the change however you need to.


The appropriate event to use to determine in the ComboBox value has changed is the SelectionChanged event. However this event can also fire when ItemsSource is assigned so its not an absolute guide to change by the user but its very close.

One approach would be to assign an event handler to SelectionChanged in the user control load event or at some other point where you know the ItemsSource has be assigned.


Did you try using the xxxCombo_DropDownClosed event?


I think I didn't make the question clear : I wanted to make the distinction between the user and code. I hope I clarified this with the last edit.

I guess there is no easy way to do this. My solution was to set some flag from the code that changed the ComboBox value. This was easy since it was my code :).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜