Set binding, based on trigger

13 באוקטובר 2008

אין תגובות

[This blog was migrated. You will not be able to comment here.
The new URL of this post is http://khason.net/blog/set-binding-based-on-trigger/]


Let’s say, that you want to set binding. However, you want to set it by using some trigger. Wait! Why I need it? Let’s say, that I have some very special object, that actually has no hard-coded properties. All it’s properties are created during the runtime, based on some trigger. In this case, I still want to use binding, however, if the property does not exists, I cannot do it. In this special case (that we’ll probably speak more later), we can use this class – TriggerBinding.

How to do it? How to extend build-in binding and add such functionality? Simple. All we need is to write custom markup extension. So, we’ll create new one

public class TriggerBindingExtension : MarkupExtension

And expose all regular binding properties + three custom – TriggerSource, TriggerSourceName and Trigger itself

public IValueConverter Converter { get; set; }
public object ConverterParameter { get; set; }
public string ElementName { get; set; }
public RelativeSource RelativeSource { get; set; }
public object Source { get; set; }
public string TriggerSourceName { get; set; }
public UIElement TriggerSource { get; set; }
[TypeConverter(typeof(RoutedEventConverter))]
public RoutedEvent Trigger { get; set; }
public bool ValidatesOnDataErrors { get; set; }
public bool ValidatesOnExceptions { get; set; }
public string StringFormat { get; set; }
[TypeConverter(typeof(EnumConverter))]
public BindingMode Mode { get; set; }
[TypeConverter(typeof(PropertyPathConverter))]
public PropertyPath Path { get; set; }
[TypeConverter(typeof(BooleanConverter))]
public bool IsAsync { get; set; }
public string XPath { get; set; }
[TypeConverter(typeof(CultureInfoIetfLanguageTagConverter))]
public CultureInfo ConverterCulture { get; set; }

Next step is to override ProvideValue method of MarkupExtension and create the actual binding. We still do not want to bind it to any property

public override sealed object ProvideValue(IServiceProvider serviceProvider) {
        var _valueProvider = serviceProvider.GetService(typeof(IProvideValueTarget)) as IProvideValueTarget;
        if (_valueProvider != null) {
           var _bindingTarget = _valueProvider.TargetObject as FrameworkElement;
           var _bindingProperty = _valueProvider.TargetProperty as DependencyProperty;
           var _binding = new Binding();
           _binding.Path = Path;
           _binding.XPath = XPath;
           _binding.Mode = Mode;
           _binding.Converter = Converter;
           _binding.ConverterCulture = ConverterCulture;
           _binding.ConverterParameter = ConverterParameter;
           _binding.IsAsync = IsAsync;
           if (ElementName != null) _binding.ElementName = ElementName;
           if (RelativeSource != null) _binding.RelativeSource = RelativeSource;
           if (Source != null) _binding.Source = Source;
           _binding.ValidatesOnDataErrors = ValidatesOnDataErrors;
           _binding.ValidatesOnExceptions = ValidatesOnExceptions;
           _binding.StringFormat = StringFormat;

Now, if we have TriggerSource and Trigger, everything fine, we can set subscribe to the trigger event and wire binding in the handler

private RoutedEventHandler _bindigHandler;

private void _registerHandler(FrameworkElement bindingTarget, DependencyProperty bindingProperty, Binding binding) {
   if (_bindigHandler == default(RoutedEventHandler)) {
      _bindigHandler = (RoutedEventHandler)(delegate {
         var _oldValue = bindingTarget.GetValue(bindingProperty);
         bindingTarget.SetBinding(bindingProperty, binding);
         if (_oldValue != bindingProperty.DefaultMetadata.DefaultValue) {
            bindingTarget.SetValue(bindingProperty, _oldValue);
            BindingOperations.GetBindingExpression(bindingTarget, bindingProperty).UpdateSource();
         }
      });
      TriggerSource = TriggerSource ?? bindingTarget;
      if (Trigger != null && TriggerSource != null) {
         TriggerSource.AddHandler(Trigger, _bindigHandler);
      } else {
         _bindigHandler.Invoke(this, null);
      }
   }
}

However, we also want to be able to seek Logical tree and find trigger source by it’s name. Let’s say this way

<TextBox Name="one" Text="{l:TriggerBinding ElementName=two, Path=Text, Mode=TwoWay, TriggerSourceName=three, Trigger=Button.Click}"/>
<TextBox Name="two"/>
<Button Name="three" Content="Click to trigger binding"/>

What’s the problem? We also have FrameworkElement.FindName method and LogicalTreeHelper.FindLogicalNode. But wait, in our case, when TextBox “one” is initialized, we still have no Button “three”, so we have to wait until the root element of TextBox will be fully loaded and then try to find logical node. For this purpose, we have very useful extended method

public static DependencyObject GetRoot(this DependencyObject current) {
         var _root = current;
         do { _root = LogicalTreeHelper.GetParent(_root); if (_root != null) current = _root; } while (_root != null);
         return current;
      }

So, all we have to do is to subscribe to Loaded event of the root and then wire the trigger for set binding

if (!string.IsNullOrEmpty(TriggerSourceName)) {
   var _root = _bindingTarget.GetRoot() as FrameworkElement;
   _root.Loaded += delegate {
      TriggerSource = LogicalTreeHelper.FindLogicalNode(_root, TriggerSourceName) as UIElement;
      _registerHandler(_bindingTarget, _bindingProperty, _binding);
   };
} else { _registerHandler(_bindingTarget, _bindingProperty, _binding); }

We done. Have a good day and be good people.

BTW, it’s also possible to postpone binding by using timer. Paul Stovell has very good sample of how to perform it.

Source code for this article

הוסף תגובה
facebook linkedin twitter email

כתיבת תגובה

האימייל לא יוצג באתר. (*) שדות חובה מסומנים