/// <summary> /// Creates a binding on a <see cref="Parameter"/>. /// </summary> /// <param name="target">The target to which the message is applied.</param> /// <param name="parameter">The parameter object.</param> /// <param name="elementName">The name of the element to bind to.</param> /// <param name="path">The path of the element to bind to.</param> /// <param name="bindingMode">The binding mode to use.</param> public static void BindParameter(FrameworkElement target, Parameter parameter, string elementName, string path, BindingMode bindingMode) { #if XFORMS var element = elementName == "$this" ? target : null; if (element == null) { return; } if (string.IsNullOrEmpty(path)) { path = ConventionManager.GetElementConvention(element.GetType()).ParameterProperty; } var binding = new Binding(path) { Source = element, Mode = bindingMode }; parameter.SetBinding(Parameter.ValueProperty, binding); #else var element = elementName == "$this" ? target : BindingScope.GetNamedElements(target).FindName(elementName); if (element == null) { return; } if (string.IsNullOrEmpty(path)) { path = ConventionManager.GetElementConvention(element.GetType()).ParameterProperty; } #if WINDOWS_UWP var binding = new Binding { Path = new PropertyPath(path), Source = element, Mode = bindingMode }; #else var binding = new Binding(path) { Source = element, Mode = bindingMode }; #endif #if !WINDOWS_UWP binding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged; #endif BindingOperations.SetBinding(parameter, Parameter.ValueProperty, binding); #endif }
/// <summary> /// Creates a binding on a <see cref="Parameter"/>. /// </summary> /// <param name="target">The target to which the message is applied.</param> /// <param name="parameter">The parameter object.</param> /// <param name="elementName">The name of the element to bind to.</param> /// <param name="path">The path of the element to bind to.</param> /// <param name="bindingMode">The binding mode to use.</param> public static void BindParameter(FrameworkElement target, Parameter parameter, string elementName, string path, BindingMode bindingMode) { var element = elementName == "$this" ? target : BindingScope.GetNamedElements(target).FindName(elementName); if (element == null) { return; } if (string.IsNullOrEmpty(path)) { path = ConventionManager.GetElementConvention(element.GetType()).ParameterProperty; } #if WinRT var binding = new Binding { Path = new PropertyPath(path), Source = element, Mode = bindingMode }; #else var binding = new Binding(path) { Source = element, Mode = bindingMode }; #endif #if (SILVERLIGHT && !SL5) var expression = (BindingExpression)BindingOperations.SetBinding(parameter, Parameter.ValueProperty, binding); var field = element.GetType().GetField(path + "Property", BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy); if (field == null) { return; } ConventionManager.ApplySilverlightTriggers(element, (DependencyProperty)field.GetValue(null), x => expression, null, null); #else #if !WinRT binding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged; #endif BindingOperations.SetBinding(parameter, Parameter.ValueProperty, binding); #endif }
void ElementLoaded(object sender, RoutedEventArgs e) { UpdateContext(); DependencyObject currentElement; if (context.View == null) { currentElement = AssociatedObject; while (currentElement != null) { if (Action.HasTargetSet(currentElement)) { break; } currentElement = BindingScope.GetVisualParent(currentElement); } } else { currentElement = context.View; } #if NET || NETCORE var binding = new Binding { Path = new PropertyPath(Message.HandlerProperty), Source = currentElement }; #elif WINDOWS_UWP var binding = new Binding { Source = currentElement }; #elif NET5_0_WINDOWS const string bindingText = "<Binding xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation\' xmlns:cal='clr-namespace:Caliburn.Micro;assembly=Caliburn.Micro.Platform' Path='(cal:Message.Handler)' />"; StringReader stringReader = new StringReader(bindingText); XmlReader xmlReader = XmlReader.Create(stringReader); var binding = (Binding)XamlReader.Load(xmlReader); binding.Source = currentElement; #else const string bindingText = "<Binding xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation\' xmlns:cal='clr-namespace:Caliburn.Micro;assembly=Caliburn.Micro.Platform' Path='(cal:Message.Handler)' />"; var binding = (Binding)XamlReader.Load(bindingText); binding.Source = currentElement; #endif BindingOperations.SetBinding(this, HandlerProperty, binding); }