0

I want to handle the DropDownOpened event of ComboBox in my ViewModel. How can I do that.

Basically my ItemSource of Comboxbox is bound to a collection. This collection can change, I do not know when it changes. So what I want to do is, I want to repopulate ComboBox every time User Clicks on the combobox (to open the dropdown). How can I do that in ViewModel.

Many Thanks in advance.

Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
WAQ
  • 2,556
  • 6
  • 45
  • 86
  • `collection can change, I do not know when it changes. ` The collection has an event [`CollectionChanged`](http://msdn.microsoft.com/en-us/library/system.collections.specialized.inotifycollectionchanged.aspx). [Sample](http://stackoverflow.com/questions/4587448/collectionchanged-sample). This is what you need? – Anatoliy Nikolaev Aug 15 '13 at 11:50

1 Answers1

0

In my opinion, the best way to handle UI events in WPF using view models is to implement Attached Properties. Here is an example that I use to monitor the LostFocus event... instead of passing the EventArgs object through to the view model, I am exposing a basic instance of ICommand (which you can implement in your view model) that will be called when the focus is lost from the relating TextBox:

public static DependencyProperty OnLostFocusProperty = DependencyProperty.
RegisterAttached("OnLostFocus", typeof(ICommand), typeof(TextBoxProperties), new 
UIPropertyMetadata(null, OnLostFocusChanged));

public static ICommand GetOnLostFocus(DependencyObject dependencyObject)
{
    return (ICommand)dependencyObject.GetValue(OnLostFocusProperty);
}

public static void SetOnLostFocus(DependencyObject dependencyObject, ICommand value)
{
    dependencyObject.SetValue(OnLostFocusProperty, value);
}

public static void OnLostFocusChanged(DependencyObject dependencyObject, 
DependencyPropertyChangedEventArgs e)
{
    TextBox textBox = dependencyObject as TextBox;
    if (e.OldValue == null && e.NewValue != null) textBox.LostFocus += TextBox_LostFocus;
    else if (e.OldValue != null && e.NewValue == null) textBox.LostFocus -= TextBox_LostFocus;
}

private static void TextBox_LostFocus(object sender, RoutedEventArgs e)
{
    TextBox textBox = sender as TextBox;
    ICommand command = GetOnLostFocus(textBox);
    if (command != null && command.CanExecute(textBox)) command.Execute(textBox);
    e.Handled = false;
}

You would use it like this:

<TextBox Text="{Binding SomeValue}" Attached:TextBoxProperties.OnLostFocus="{Binding 
YourCommandName}" />

I'm sure that you can make a few changes to be able to apply it to your situation. If you are not familiar with Attached Properties, please view the Attached Properties Overview page at MSDN.

Sheridan
  • 68,826
  • 24
  • 143
  • 183