Im vorherigen Artikel hab ich die Klasse BindingHelper von Thomas vorgestellt. Diese ermöglich ein UpdateSourceTrigger “OnPropertyChanged” an einer TextBox in Silverlight.

Ich habe die Klasse mal für eine ComboBox erweitert.

/// <summary> /// Supports a PropertyChanged-Trigger for DataBindings /// in Silverlight. Works just for TextBoxes /// (C) Thomas Claudius Huber 2009 /// http://www.thomasclaudiushuber.com /// extended from Mario Priebe 2011 for ComboBox SelectedItem /// http://www.biggle.de /// </summary> public class BindingHelper { public static bool GetUpdateSourceOnChange(DependencyObject obj) { return (bool)obj.GetValue(UpdateSourceOnChangeProperty); } public static void SetUpdateSourceOnChange(DependencyObject obj, bool value) { obj.SetValue(UpdateSourceOnChangeProperty, value); } // Using a DependencyProperty as the backing store for … public static readonly DependencyProperty UpdateSourceOnChangeProperty = DependencyProperty.RegisterAttached("UpdateSourceOnChange", typeof(bool), typeof(BindingHelper), new PropertyMetadata(false, OnPropertyChanged)); /// <summary> /// Called when [property changed]. /// </summary> /// <param name="obj">The obj.</param> /// <param name="e">The <see cref="System.Windows.DependencyPropertyChangedEventArgs"/> instance containing the event data.</param> private static void OnPropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e) { if (obj is ComboBox) { var cb = obj as ComboBox; if ((bool)e.NewValue) cb.SelectionChanged += (cb_SelectionChanged); else cb.SelectionChanged -= (cb_SelectionChanged); } else if (obj is TextBox) { var txt = obj as TextBox; if ((bool)e.NewValue) txt.TextChanged += OnTextChanged; else txt.TextChanged -= OnTextChanged; } } /// <summary> /// Called when [text changed]. /// </summary> /// <param name="sender">The sender.</param> /// <param name="e">The <see cref="System.Windows.Controls.TextChangedEventArgs"/> instance containing the event data.</param> static void OnTextChanged(object sender, TextChangedEventArgs e) { var txt = sender as TextBox; if (txt == null) return; var be = txt.GetBindingExpression(TextBox.TextProperty); if (be != null) be.UpdateSource(); } /// <summary> /// Handles the SelectionChanged event of the combobox control. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="System.Windows.Controls.SelectionChangedEventArgs"/> instance containing the event data.</param> static void cb_SelectionChanged(object sender, SelectionChangedEventArgs e) { var cb = sender as ComboBox; if (cb == null) return; var be = cb.GetBindingExpression(ComboBox.SelectedItemProperty); if (be != null) be.UpdateSource(); } }

Im gebundenen ViewModel definiere ich ein Property vom Typ, welcher in der ComboBox verwendet wird.

private TourData tourFilter; public TourData TourFilter { get { return tourFilter; } set { tourFilter = value; OnPropertyChanged("TourFilter"); } }

Verwendet wird das Ganze dann wie folgt:

<ComboBox ItemsSource="{Binding ElementName=TourDataDomainContext, Path=Data}" DisplayMemberPath="TourName" SelectedValuePath="TourId" SelectedItem="{Binding Source={StaticResource DailyStatsViewModel}, Path=TourFilter, Mode=TwoWay}" helper:BindingHelper.UpdateSourceOnChange="True" />

Viel Spaß beim entwickeln : )

UpdateSource-Trigger für Silverlight ComboBox
Markiert in: