CSLA.NET 5.4.2
CSLA .NET is a software development framework that helps you build a reusable, maintainable object-oriented business layer for your app.
Csla.Iosui.Ios/Binding/Binding.cs
Go to the documentation of this file.
1//-----------------------------------------------------------------------
2// <copyright file="Binding.cs" company="Marimer LLC">
3// Copyright (c) Marimer LLC. All rights reserved.
4// Website: https://cslanet.com
5// </copyright>
6// <summary>Contains an individual binding to tie a property on an Axml view to the property on a supplied object</summary>
7//-----------------------------------------------------------------------
8
9using System;
10using System.ComponentModel;
11using System.Linq;
12using System.Reflection;
13#if __UNIFIED__
14using UIKit;
15#else
16using MonoTouch.UIKit;
17#endif
18
20{
24 public class Binding : IDisposable
25 {
29 public BindingDirection BindingDirection { get; private set; }
30
34 public UIView Target { get; private set; }
35
39 public object Source { get; private set; }
40
44 public System.Reflection.PropertyInfo TargetProperty { get; private set; }
45
49 public System.Reflection.PropertyInfo SourceProperty { get; private set; }
50
54 public Func<object, object> Convert { get; private set; }
55
59 public Func<object, object> ConvertBack { get; private set; }
60
69 public Binding(UIView target, string targetProperty, object source, string sourceProperty, BindingDirection bindingDirection)
70 : this(target, targetProperty, source, sourceProperty, null, null, bindingDirection)
71 { }
72
82 public Binding(UIView target, string targetProperty, object source, string sourceProperty, Func<object, object> convert, BindingDirection bindingDirection)
83 : this(target, targetProperty, source, sourceProperty, convert, null, bindingDirection)
84 { }
85
96 public Binding(UIView target, string targetProperty, object source, string sourceProperty, Func<object, object> convert, Func<object, object> convertBack, BindingDirection bindingDirection)
97 {
98 BindingDirection = bindingDirection;
99 Target = target;
100 Source = source;
101 TargetProperty = Target.GetType().GetProperty(targetProperty);
102 SourceProperty = Source.GetType().GetProperty(sourceProperty);
103 Convert = convert;
104 ConvertBack = convertBack;
105
106 var editableControl = Target as UIControl;
107 if (editableControl != null)
108 editableControl.EditingDidEnd += Target_EditingDidEnd;
109
110 var inpc = Source as INotifyPropertyChanged;
111 if (inpc != null)
112 inpc.PropertyChanged += Source_PropertyChanged;
113
114 UpdateTarget();
115 }
116
122 void Source_PropertyChanged(object sender, PropertyChangedEventArgs e)
123 {
124 UpdateTarget();
125 }
126
132 void Target_EditingDidEnd(object sender, EventArgs e)
133 {
134 var control = sender as UIControl;
135 if (control != null && !control.Selected)
136 {
137 UpdateSource();
138 }
139 }
140
144 public void UpdateSource()
145 {
146 if (this.BindingDirection == BindingDirection.OneWay)
147 return;
148 var value = TargetProperty.GetValue(Target, null);
149 if (ConvertBack != null)
150 value = ConvertBack(value);
151 var converted = Utilities.CoerceValue(
152 SourceProperty.PropertyType, TargetProperty.PropertyType, null, value);
153 SourceProperty.SetValue(Source,
154 converted,
155 null);
156 }
157
161 public void UpdateTarget()
162 {
163 var value = SourceProperty.GetValue(Source, null);
164 if (Convert != null)
165 value = Convert(value);
166 var converted = Utilities.CoerceValue(
167 TargetProperty.PropertyType, SourceProperty.PropertyType, null, value);
168 TargetProperty.SetValue(Target,
169 converted,
170 null);
171 }
172
176 public void Dispose()
177 {
178 var editableControl = Target as UIControl;
179 if (editableControl != null)
180 editableControl.EditingDidEnd -= Target_EditingDidEnd;
181
182 var inpc = Source as INotifyPropertyChanged;
183 if (inpc != null)
184 inpc.PropertyChanged -= Source_PropertyChanged;
185 Target = null;
186 Source = null;
187 TargetProperty = null;
188 SourceProperty = null;
189 }
190 }
191}
Contains an individual binding to tie a property on an Axml view to the property on a supplied object...
Binding(UIView target, string targetProperty, object source, string sourceProperty, Func< object, object > convert, Func< object, object > convertBack, BindingDirection bindingDirection)
Creates a new instance of the Binding class.
Func< object, object > Convert
A reference to a function to do a custom conversion from the source property to the target property.
System.Reflection.PropertyInfo TargetProperty
The PropertyInfo for the property on the target view that is being bound to.
Binding(UIView target, string targetProperty, object source, string sourceProperty, BindingDirection bindingDirection)
Creates a new instance of the Binding class.
UIView Target
The iOS view that is used by the binding.
Func< object, object > ConvertBack
A reference to a function to do a custom conversion from the target property to the source property.
BindingDirection BindingDirection
Indicates if this is read only or two way binding.
void Dispose()
Clears the bindings, references and event handlers.
object Source
The object that the view is bound to.
Binding(UIView target, string targetProperty, object source, string sourceProperty, Func< object, object > convert, BindingDirection bindingDirection)
Creates a new instance of the Binding class.
void UpdateTarget()
Updates the target with the current value in the source object.
System.Reflection.PropertyInfo SourceProperty
The PropertyInfo for the property on the source object that is being bound to.
void UpdateSource()
Updates the source with the current value in the target object.
PropertyInfo(string name)
Creates a new instance of this class.