public void OneWayPropertyBinding_DifferentPropertySameType_BindedCorrectly() { // Arrange ChartVM source = new ChartVM { BottomPadding = 0, LeftPadding = 0, RightPadding = 0, TopPadding = 0, }; ChartVM destination = new ChartVM { BottomPadding = 10, LeftPadding = 10, RightPadding = 10, TopPadding = 10, }; var binding = new OneWayPropertyBinding <int, int> (destination, (vm) => ((ChartVM)vm).RightPadding, (vm) => ((ChartVM)vm).BottomPadding); binding.ViewModel = source; // Act source.RightPadding = 999; // Assert Assert.AreNotEqual(source.BottomPadding, destination.BottomPadding); Assert.AreEqual(source.RightPadding, destination.BottomPadding); }
/// <summary> /// Get a list of properties in the bound view model that match the type of the selected property in the UI. /// </summary> private PropertyInfo[] GetBindableViewModelProperties(OneWayPropertyBinding target) { // TODO Rory 29/06/16: make sure we're not using an outdated list of available views return(target.GetAvailableViewModelTypes() .SelectMany(type => type.GetProperties(BindingFlags.Public | BindingFlags.Instance)) .ToArray()); }
protected override void OnEnabled() { // Initialise reference to target script targetScript = (OneWayPropertyBinding)target; viewAdapterOptionsFade = new AnimBool(ShouldShowAdapterOptions(targetScript.ViewAdapterId, out _)); viewAdapterOptionsFade.valueChanged.AddListener(Repaint); }
protected override void OnEnable() { Target = target as OneWayPropertyBinding; base.OnEnable(); ViewAdapterOptions = serializedObject.FindProperty("_viewAdapterOptions"); }
public static void BindNativeProperty <TValue>( IObservable <bool> isRooted, IScheduler dispatcher, string name, IObservable <TValue> value, Action <TValue> update) { OneWayPropertyBinding <TValue> .Bind(isRooted, dispatcher, value, update, name); }
/// <summary> /// Draws the dropdown menu for selectng a property in the UI to bind to. /// </summary> private int ShowUIPropertySelector(OneWayPropertyBinding targetScript) { return(EditorGUILayout.Popup( new GUIContent("View property"), uiProperties.Select(prop => prop.PropertyInfo.Name) .ToList() .IndexOf(targetScript.uiPropertyName), uiProperties.Select(prop => new GUIContent(prop.PropertyInfo.ReflectedType.Name + "/" + prop.PropertyInfo.Name + " : " + prop.PropertyInfo.PropertyType.Name)).ToArray())); }
private void OnEnable() { // Initialise reference to target script targetScript = (OneWayPropertyBinding)target; Type adapterType; viewAdapterOptionsFade = new AnimBool( ShouldShowAdapterOptions(targetScript.ViewAdapterTypeName, out adapterType) ); viewAdapterOptionsFade.valueChanged.AddListener(Repaint); }
public SliderViewCommandBinding(SliderView sliderView, Button showButton, Func <IViewModel, Command <double> > commandFunc, Expression <Func <IViewModel, double> > propertyExpression, Action <double> updateViewAction) : base(commandFunc, null) { this.sliderView = sliderView; this.commandFunc = commandFunc; this.showButton = showButton; WireShowButtonClickEvent(showButton); if (propertyExpression != null && updateViewAction != null) { propBinding = new OneWayPropertyBinding <double, double> (propertyExpression, updateViewAction); } }
public void OneWayPropertyBinding_ChangedSource_BindedWithLast() { // Arrange CountLimitationVM source = new CountLimitationVM { Model = new CountLicenseLimitation { DisplayName = "source", RegisterName = "source", Count = 1, Maximum = 5, Enabled = true } }; CountLimitationVM source2 = new CountLimitationVM { Model = new CountLicenseLimitation { DisplayName = "source2", RegisterName = "source2", Count = 2, Maximum = 10, Enabled = true } }; CountLimitationVM destination = new CountLimitationVM { Model = new CountLicenseLimitation { DisplayName = "destination", RegisterName = "destination", Count = 0, Maximum = 0, Enabled = false } }; var binding = new OneWayPropertyBinding <int, int> (destination, (vm) => ((CountLimitationVM)vm).Count, (vm) => ((CountLimitationVM)vm).Count); // Act binding.ViewModel = source; binding.ViewModel = source2; source.Count = 777; source2.Count = 999; // Assert Assert.AreEqual("destination", destination.DisplayName); Assert.AreEqual("source", source.DisplayName); Assert.AreEqual("source2", source2.DisplayName); Assert.AreEqual(source2, binding.ViewModel); Assert.AreEqual(source2.Count, destination.Count); Assert.AreNotEqual(source.Count, source2.Count); }
public static void Main() { Address address = new Address { City = "Boston" }; Contact aContact = new Contact { HomeAddress = address }; PrintModel printModel = new PrintModel { HomeCityPrintObj = new PrintProp("Home City") }; OneWayPropertyBinding homeCityBinding = new OneWayPropertyBinding { SourcePropertyGetter = new CompositePathGetter<object> ( new List<BindingPathLink<object>> { new BindingPathLink<object>("HomeAddress"), new BindingPathLink<object>("City") }, null ) { TheObj = aContact } , TargetPropertySetter = new CompositePathSetter<object> ( new List<BindingPathLink<object>> { new BindingPathLink<object>("HomeCityPrintObj"), new BindingPathLink<object>("PropValueToPrint") } ) { TheObj = printModel } }; homeCityBinding.Bind(); printModel.Print(); address.City = "Miami"; printModel.Print(); }
/// <summary> /// Draws the dropdown menu for picking a property in the view model to bind to. /// </summary> private void ShowViewModelPropertyDropdown(OneWayPropertyBinding target, PropertyInfo[] bindableProperties, Type viewPropertyType, Rect position) { var selectedIndex = Array.IndexOf( bindableProperties.Select(p => p.ReflectedType.Name + p.Name).ToArray(), target.viewModelName + target.viewModelPropertyName ); var options = bindableProperties.Select(p => new InspectorUtils.MenuItem( new GUIContent(p.ReflectedType.Name + "/" + p.Name + " : " + p.PropertyType.Name), p.PropertyType == viewPropertyType ) ).ToArray(); InspectorUtils.ShowCustomSelectionMenu(index => SetViewModelProperty(target, bindableProperties[index]), options, selectedIndex, position); }
private void ShowViewModelPropertySelector(OneWayPropertyBinding target, PropertyInfo[] bindableProperties, Type viewPropertyType) { var buttonContent = new GUIContent(target.viewModelPropertyName); EditorGUILayout.BeginHorizontal(); EditorGUILayout.PrefixLabel("View model property"); var dropdownPosition = GUILayoutUtility.GetLastRect(); dropdownPosition.x += dropdownPosition.width; if (GUILayout.Button(buttonContent, EditorStyles.popup)) { ShowViewModelPropertyDropdown(target, bindableProperties, viewPropertyType, dropdownPosition); } EditorGUILayout.EndHorizontal(); }
public void OneWayPropertyBinding_RemovedSourceAfterTrigger_Unbinded() { // Arrange CountLimitationVM source = new CountLimitationVM { Model = new CountLicenseLimitation { DisplayName = "source", RegisterName = "source", Count = 1, Maximum = 5, Enabled = true } }; CountLimitationVM destination = new CountLimitationVM { Model = new CountLicenseLimitation { DisplayName = "destination", RegisterName = "destination", Count = 0, Maximum = 0, Enabled = false } }; var binding = new OneWayPropertyBinding <int, int> (destination, (vm) => ((CountLimitationVM)vm).Count, (vm) => ((CountLimitationVM)vm).Count); // Act binding.ViewModel = source; source.Count = 999; binding.ViewModel = null; source.Count = 777; // Assert Assert.AreEqual("destination", destination.DisplayName); Assert.AreEqual("source", source.DisplayName); Assert.AreEqual(null, binding.ViewModel); Assert.AreNotEqual(source.Count, destination.Count); Assert.AreEqual(999, destination.Count); }
public void OneWayPropertyBinding_SetSource_DestinationUpdated() { // Arrange CountLimitationVM source = new CountLimitationVM { Model = new CountLicenseLimitation { DisplayName = "source", RegisterName = "source", Count = 1, Maximum = 5, Enabled = true } }; CountLimitationVM destination = new CountLimitationVM { Model = new CountLicenseLimitation { DisplayName = "destination", RegisterName = "destination", Count = 0, Maximum = 0, Enabled = false } }; var binding = new OneWayPropertyBinding <int, int> (destination, (vm) => ((CountLimitationVM)vm).Count, (vm) => ((CountLimitationVM)vm).Count); // Act binding.ViewModel = source; // Assert Assert.AreEqual("destination", destination.DisplayName); Assert.AreEqual("source", source.DisplayName); Assert.AreEqual(source, binding.ViewModel); Assert.AreEqual(source.Count, destination.Count); Assert.AreEqual(1, destination.Count); }
static void Main(string[] args) { //#region plain props //ParentDataClass parentDataObj = new ParentDataClass(); //parentDataObj.TheData = new DataClass { MyStringProp = "Hello World" }; //object targetObj = new object(); //OneWayPropertyBindingBase<object, object> binding = new OneWayPropertyBindingBase<object, object>(); //CompositePathGetter pathGetter = // new CompositePathGetter // ( // new BindingPathLink<object>[] // { // new BindingPathLink<object>("TheData"), // new BindingPathLink<object>("MyStringProp"), // }, // "blabla" // ); //pathGetter.TheObj = parentDataObj; //binding.SourcePropertyGetter = pathGetter; //binding.TargetPropertySetter = new APropSetter<object>(targetObj, MyAProperty); //binding.Bind(); //Console.WriteLine(MyAProperty.GetProperty(targetObj)); //parentDataObj.TheData.MyStringProp = "Hi World"; //Console.WriteLine(MyAProperty.GetProperty(targetObj)); //parentDataObj.TheData = new DataClass { MyStringProp = "bye bye" }; //Console.WriteLine(MyAProperty.GetProperty(targetObj)); //parentDataObj.TheData = null; //Console.WriteLine(MyAProperty.GetProperty(targetObj)); //#endregion plain props #region plain and attached props TopLevelObj parentDataObj = new TopLevelObj(); DataClass dataObject = new DataClass{ MyStringProp = "Hello World"}; MyAProperty.SetProperty(parentDataObj, dataObject); object targetObj = new object(); OneWayPropertyBinding<object, object> binding = new OneWayPropertyBinding<object, object>(); CompositePathGetter<object> pathGetter = new CompositePathGetter<object> ( new BindingPathLink<object>[] { new BindingPathLink<object>(MyAProperty), new BindingPathLink<object>("MyStringProp") }, "blabla" ); pathGetter.TheObj = parentDataObj; binding.SourcePropertyGetter = pathGetter; binding.TargetPropertySetter = new APropSetter<object>(targetObj, MyAProperty); binding.Bind(); Console.WriteLine(MyAProperty.GetProperty(targetObj)); dataObject.MyStringProp = "Hi World"; Console.WriteLine(MyAProperty.GetProperty(targetObj)); MyAProperty.SetProperty( parentDataObj, new DataClass { MyStringProp = "bye bye" }); Console.WriteLine(MyAProperty.GetProperty(targetObj)); MyAProperty.ClearAProperty(parentDataObj); Console.WriteLine(MyAProperty.GetProperty(targetObj)); #endregion plain and attached props }
/// <summary> /// Set up the viewModelName and viewModelPropertyName in the OneWayPropertyBinding we're editing. /// </summary> private void SetViewModelProperty(OneWayPropertyBinding target, PropertyInfo property) { target.viewModelName = property.ReflectedType.Name; target.viewModelPropertyName = property.Name; }
static void Main(string[] args) { #region Plain C# to Plain C# property binding Console.WriteLine("\n\nTesting binding from plain property to another plain property\n"); // initialize test objects MyTestDataClass sourceObj = new MyTestDataClass { MyStringProp = "Hello World" }; MyTestDataClass targetObj = new MyTestDataClass(); OneWayPropertyBindingWithPath<string, string> plainToPlainPropBinding = new OneWayPropertyBindingWithPath<string, string>(); plainToPlainPropBinding.SourceObj = sourceObj; plainToPlainPropBinding.SourcePPath = new BindingPathLink<string>("MyStringProp"); plainToPlainPropBinding.TargetObj = targetObj; plainToPlainPropBinding.TargetPPath = new BindingPathLink<string>("MyStringProp"); // bind the two properties. plainToPlainPropBinding.Bind(); // verify that the binding changed the target property // to be the same as the source property Console.Write("Testing target property change after binding operation: "); Console.WriteLine(targetObj.MyStringProp); // should print Hello World; // let us change the source property and verify that target property also changes sourceObj.MyStringProp = "Hi World"; Console.Write("Testing target property change after the source property change: "); Console.WriteLine(targetObj.MyStringProp); // should print Hi World; #endregion Plain C# to Plain C# property binding #region AProperty to AProperty binding Console.WriteLine("\n\nTesting binding from plain property to another plain property\n"); AProperty<object, string> myAProperty = new AProperty<object, string>(); // reinitialize test objects sourceObj = new MyTestDataClass(); targetObj = new MyTestDataClass(); // set AProperty on the source object before the binding myAProperty.SetProperty(sourceObj, "Hello World"); OneWayPropertyBinding<string, string> aPropToAPropBinding = new OneWayPropertyBinding<string, string>(); aPropToAPropBinding.SourceObj = sourceObj; aPropToAPropBinding.SourcePPath = new BindingPathLink<string>(myAProperty); aPropToAPropBinding.TargetObj = targetObj; aPropToAPropBinding.TargetPPath = new BindingPathLink<string>(myAProperty); aPropToAPropBinding.Bind(); Console.Write("Testing target property change after binding operation: "); Console.WriteLine(myAProperty.GetProperty(targetObj)); // change the source property myAProperty.SetProperty(sourceObj, "Hi World"); Console.Write("Testing target property change after the source property change: "); Console.WriteLine(myAProperty.GetProperty(targetObj)); #endregion AProperty to AProperty binding #region plain property to AProperty binding Console.WriteLine("\n\nTesting binding from plain property to AProp\n"); // reinitialize test objects sourceObj = new MyTestDataClass { MyStringProp = "Hello World" }; targetObj = new MyTestDataClass(); OneWayPropertyBinding<string, string> plainToAPropBinding = new OneWayPropertyBinding<string, string>(); plainToAPropBinding.SourcePPath = new BindingPathLink<string>("MyStringProp") { DefaultValue = "blablabla" }; plainToAPropBinding.TargetObj = targetObj; plainToAPropBinding.TargetPPath = new BindingPathLink<string>(myAProperty); plainToAPropBinding.Bind(); Console.Write("Testing target property change after binding operation without source object (default value will be used): "); Console.WriteLine(myAProperty.GetProperty(targetObj)); plainToAPropBinding.SourceObj = sourceObj; Console.Write("Testing target property change after binding operation with source object: "); Console.WriteLine(myAProperty.GetProperty(targetObj)); sourceObj.MyStringProp = "Hi World"; Console.Write("Testing target property change after the source property change: "); Console.WriteLine(myAProperty.GetProperty(targetObj)); #endregion plain property to AProperty binding #region AProperty to plain property binding Console.WriteLine("\n\nTesting binding from AProp to plain property\n"); // reinitialize test objects sourceObj = new MyTestDataClass(); targetObj = new MyTestDataClass(); myAProperty.SetProperty(sourceObj, "Hello World"); OneWayPropertyBinding<string, string> aPropToPlainBinding = new OneWayPropertyBinding<string, string>(); aPropToPlainBinding.SourceObj = sourceObj; aPropToPlainBinding.SourcePPath = new BindingPathLink<string>(myAProperty); aPropToPlainBinding.TargetObj = targetObj; aPropToPlainBinding.TargetPPath = new BindingPathLink<string>("MyStringProp"); aPropToPlainBinding.Bind(); Console.Write("Testing target property change after binding operation: "); Console.WriteLine(targetObj.MyStringProp); myAProperty.SetProperty(sourceObj, "Hi World"); Console.Write("Testing target property change after the source property change: "); Console.WriteLine(targetObj.MyStringProp); #endregion AProperty to plain property binding }
static void Main(string[] args) { /* // first special case - composite path getter and setter with only one link // create source object DataClass sourceDataObj = new DataClass { MyStringProp = "Hello World" }; // create target object DataClass targetDataObject = new DataClass(); // create the binding OneWayPropertyBindingBase<object, object> binding = new OneWayPropertyBindingBase<object, object>(); CompositePathGetter sourcePathGetter = new CompositePathGetter ( new BindingPathLink<object>[] { new BindingPathLink<object>("MyStringProp"), }, "A Default String" ); sourcePathGetter.TheObj = sourceDataObj; binding.SourcePropertyGetter = sourcePathGetter; CompositePathSetter targetPathSetter = new CompositePathSetter ( new BindingPathLink<object>[] { new BindingPathLink<object>("MyStringProp") } ); targetPathSetter.TheObj = targetDataObject; binding.TargetPropertySetter = targetPathSetter; binding.Bind(); Console.WriteLine(targetDataObject.MyStringProp); sourceDataObj.MyStringProp = "Hi World"; Console.WriteLine(targetDataObject.MyStringProp); */ // second special case - source composite getter with no links at all // (should simply pass the source object to the target); CompositePathGetter<object> sourcePathGetter = new CompositePathGetter<object>(new List<BindingPathLink<object>>(), "a default string"); sourcePathGetter.TheObj = "Hello World"; // create target object DataClass targetDataObject = new DataClass(); OneWayPropertyBinding<object, object> binding = new OneWayPropertyBinding<object, object>(); binding.SourcePropertyGetter = sourcePathGetter; CompositePathSetter<object> targetPathSetter = new CompositePathSetter<object> ( new BindingPathLink<object>[] { new BindingPathLink<object>("MyStringProp") } ); targetPathSetter.TheObj = targetDataObject; binding.TargetPropertySetter = targetPathSetter; binding.Bind(); Console.WriteLine(targetDataObject.MyStringProp); sourcePathGetter.TheObj = "Hi World"; Console.WriteLine(targetDataObject.MyStringProp); }
public void Bind() { _binding = new OneWayPropertyBinding<object, object>(); //_sourcePathGetter = new DependencyPropertyGetter<object> //( // MainWindow.MyTestDPPropertyProperty //); _sourcePathGetter = new CompositePathGetter<object> ( new BindingPathLink<object>[] { new VisualBindingPathLink<object>(MainWindow.MyTestDPPropertyProperty) }, "The default value" ); _sourcePathGetter.TheObj = this; _binding.SourcePropertyGetter = _sourcePathGetter; CompositePathSetter<object> targetPathSetter = new CompositePathSetter<object> ( new BindingPathLink<object>[] { new BindingPathLink<object>("MyStringProp") } ); targetPathSetter.TheObj = _myDataObj; _binding.TargetPropertySetter = targetPathSetter; _binding.Bind(); }
static void Main(string[] args) { // create source object ParentDataClass sourceDataObj = new ParentDataClass(); sourceDataObj.TheData = new DataClass { MyStringProp = "Hello World" }; // create target object ParentDataClass targetDataObject = new ParentDataClass(); targetDataObject.TheData = new DataClass(); // create the binding OneWayPropertyBinding<object, object> binding = new OneWayPropertyBinding<object, object>(); CompositePathGetter<object> sourcePathGetter = new CompositePathGetter<object> ( new BindingPathLink<object>[] { new BindingPathLink<object>("TheData"), new BindingPathLink<object>("MyStringProp"), }, "A Default String" ); sourcePathGetter.TheObj = sourceDataObj; binding.SourcePropertyGetter = sourcePathGetter; CompositePathSetter<object> targetPathSetter = new CompositePathSetter<object> ( new BindingPathLink<object>[] { new BindingPathLink<object>("TheData"), new BindingPathLink<object>("MyStringProp") } ); targetPathSetter.TheObj = targetDataObject; binding.TargetPropertySetter = targetPathSetter; binding.Bind(); Console.WriteLine(targetDataObject.TheData.MyStringProp); sourceDataObj.TheData.MyStringProp = "Hi World"; Console.WriteLine(targetDataObject.TheData.MyStringProp); sourceDataObj.TheData = new DataClass { MyStringProp = "bye bye" }; Console.WriteLine(targetDataObject.TheData.MyStringProp); sourceDataObj.TheData = null; Console.WriteLine(targetDataObject.TheData.MyStringProp); targetDataObject.TheData = new DataClass(); Console.WriteLine(targetDataObject.TheData.MyStringProp); targetDataObject.TheData.MyStringProp = "Hi"; Console.WriteLine(targetDataObject.TheData.MyStringProp); targetDataObject.TheData = new DataClass(); Console.WriteLine(targetDataObject.TheData.MyStringProp); }