If the event ComboBox_DropDownOpened
is working you can wrap it in a behavior which should look like this :
internal class ItemsSourceBindingOnOpenBehavior
{
public static readonly DependencyProperty SourceProperty =
DependencyProperty.RegisterAttached("Source", typeof(ObservableCollection<string>),
typeof(ItemsSourceBindingOnOpenBehavior),
new UIPropertyMetadata(null, OnSourceChanged));
public static ObservableCollection<string> GetSource(DependencyObject obj)
{
return (ObservableCollection<string>)obj.GetValue(SourceProperty);
}
public static void SetSource(DependencyObject obj, ObservableCollection<string> value)
{
obj.SetValue(SourceProperty, value);
}
private static void OnSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
SetSource(d);
}
private static void SetSource(DependencyObject d)
{
var cbo = d as ComboBox;
if (cbo != null) cbo.DropDownOpened += (s, a) => { cbo.ItemsSource = GetSource(cbo); };
}
}
To activate the behavior use the two provided attached properties in your XAML :
<ComboBox a:ItemsSourceBindingOnOpenBehavior.Source="{Binding ViewModelCollection}"/>