0

I'm in a situation where I need to empty a property in my model whenever someone changes a value in a combobox.

A side effect of this is, that whenever I change the value of the Combobox-Bound variable, the Combobox SelectionChanged event is triggered.

Is there anyway to know who is triggering this event. I'd like to know if it is triggered manually or by binding.

I'm looking in to the sender, but they look about the same.

Thank you,

user1841243
  • 1,663
  • 2
  • 19
  • 35
  • 2
    Most likely you are targeting the problem wrong way. Do proper bindings in your code and do stuff over there instead of relying on who changed it. Post the actual problem here what you are trying to achieve. – Rohit Vats Aug 06 '14 at 14:05

2 Answers2

0

This is a prime example of why WPF developers should use the MVVM design pattern. By relying on SelectionChanged events to control the flow of your code, you're losing your own control over what should happen and are resorting to having to know how an event is being triggered so that you can respond. That's reactive, not pro-active. As a developer, you should always know what can and will affect your code flow.

Rather, set your WPF page or control's DataContext to a viewmodel class that wraps your model and bind your combobox to the ViewModel properties, specifically SelectedItem in this case. This will simplify your coding immensely.

ViewModel

public class ViewModel : INotifyPropertyChanged 
{
    public event PropertyChangedEventHandler PropertyChanged;

    public ObservableCollection<string> Names = new ObservableCollection<string>();
    private string _selectedName;
    private YourModel _model;

    public ViewModel(YourModel model)
    {
        _model = model;
    }

    public string SelectedName
    {
        get { return _model.SelectedName; }
        set
        {
            _model.SelectedName = value;
            OnPropertyChanged();
        }
    }

    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
    }
}

XAML

<ComboBox ItemsSource="{Binding Names}" SelectedItem="{Binding SelectedName}" />
Barracoder
  • 3,696
  • 2
  • 28
  • 31
  • I have inherited an application that uses MVVM but at several places has stuff like this inside it. Unfortunately it's build in such a way that business logic and front end logic is completely mixed up. I'll probably will be breaking something If I put the logic in the viewmodel since this property is used all over the place. Since testing is also very limited and there are a few Thousand users, with a release once very 3 months, and having limited resources for refactoring, I'm currently not gonna take any risks. Though thanks for your reply. – user1841243 Aug 07 '14 at 09:20
0

Dropodownclosed is the event I best use here instead of selectionchanged. This will make sure that the event is triggered not by the data that is bound but by user interface interaction.

user1841243
  • 1,663
  • 2
  • 19
  • 35