4 using System.Collections.Generic;
7 using System.Windows.Controls;
9 using System.Windows.Data;
10 using System.Windows.Interactivity;
11 using System.ComponentModel;
13 using SiliconStudio.Core;
14 using SiliconStudio.Presentation.Core;
15 using SiliconStudio.Presentation.Extensions;
17 namespace SiliconStudio.Presentation.Behaviors
26 private IDisposable subscriber;
27 private DependencyProperty property;
32 public string PropertyName {
get; set; }
36 public BindingBase Binding {
get; set; }
40 if (
string.IsNullOrWhiteSpace(PropertyName))
41 throw new ArgumentException(
"PropertyName must be set.");
45 throw new ArgumentException(
string.Format(
"Binding must be set for {0} property of host '{1}' on behavior '{2}'.",
46 PropertyName, AssociatedObject, this.GetType().FullName));
49 property = AssociatedObject.GetDependencyProperties(
true).FirstOrDefault(dp => dp.Name == PropertyName);
51 if (property == null )
52 throw new InvalidOperationException(
string.Format(
"Impossible to find property named '{0}' on object typed '{1}'.", PropertyName, AssociatedObject.GetType()));
54 if ((Binding is Binding) ==
false)
55 throw new InvalidOperationException(
"Not supported binding type.");
57 var element = AssociatedObject as FrameworkElement;
60 throw new InvalidOperationException(
string.Format(
"Behavior of type '{0}' must be bound to objects of type '{1}'. (currently bound to object typed '{2}')",
61 this.GetType(), typeof(FrameworkElement), AssociatedObject.GetType()));
64 BindingOperations.SetBinding(AssociatedObject, property, Binding);
67 element.GotFocus += OnHostGotFocus;
68 element.LostFocus += OnHostLostFocus;
73 element.LostFocus -= OnHostLostFocus;
74 element.GotFocus -= OnHostGotFocus;
87 object value = AssociatedObject.GetValue(property);
90 BindingOperations.ClearBinding(AssociatedObject, property);
93 AssociatedObject.SetValue(property, value);
98 PushTargetValueToSource();
102 private void PushTargetValueToSource()
105 object currentValue = AssociatedObject.GetValue(property);
107 var binding = (Binding)Binding;
110 object source = binding.Source ?? ((FrameworkElement)AssociatedObject).DataContext;
112 var intermediateBinding =
new Binding
115 UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged,
118 Mode = BindingMode.OneWayToSource,
123 Converter = binding.Converter,
124 ConverterParameter = binding.ConverterParameter,
128 BindingOperations.SetBinding(AssociatedObject, property, intermediateBinding);
131 AssociatedObject.SetValue(property, currentValue);
134 BindingOperations.SetBinding(AssociatedObject, property, Binding);
override void OnAttached()
This class allows implementation of IDisposable using anonymous functions. The anonymous function wil...
Represents a behavior capable of interrupting a Binding on a UIElement when it receives focus...
override void OnDetaching()