XAML binding to complex value objects

I have a class of objects with complex values ​​that has 1) a number or read-only properties; 2) private constructor; and 3) the number of static properties of the singleton instance [so the properties of the ComplexValueObject never change, and an individual value is created once in the application life cycle].

public class ComplexValueClass { /* A number of read only properties */ private readonly string _propertyOne; public string PropertyOne { get { return _propertyOne; } } private readonly string _propertyTwo; public string PropertyTwo { get { return _propertyTwo; } } /* a private constructor */ private ComplexValueClass(string propertyOne, string propertyTwo) { _propertyOne = propertyOne; _propertyTwo = PropertyTwo; } /* a number of singleton instances */ private static ComplexValueClass _complexValueObjectOne; public static ComplexValueClass ComplexValueObjectOne { get { if (_complexValueObjectOne == null) { _complexValueObjectOne = new ComplexValueClass("string one", "string two"); } return _complexValueObjectOne; } } private static ComplexValueClass _complexValueObjectTwo; public static ComplexValueClass ComplexValueObjectTwo { get { if (_complexValueObjectTwo == null) { _complexValueObjectTwo = new ComplexValueClass("string three", "string four"); } return _complexValueObjectTwo; } } } 

I have a data context class that looks something like this:

 public class DataContextClass : INotifyPropertyChanged { private ComplexValueClass _complexValueClass; public ComplexValueClass ComplexValueObject { get { return _complexValueClass; } set { _complexValueClass = value; PropertyChanged(this, new PropertyChangedEventArgs("ComplexValueObject")); } } } 

I would like to write a XAML binding instruction for a property on my complex value object that updates the user interface whenever the entire complex value object changes. What is the best and / or most concise way to do this? I have something like:

 <Object Value="{Binding ComplexValueObject.PropertyOne}" /> 

but the user interface is not updated when ComplexValueObject changes as a whole.

+4
source share
3 answers

Your source script should work fine, because in most cases, bindings recognize notification of changes on any part of their property path. I actually tried the code you sent to confirm, and it works fine.

Are there any other difficulties that you cannot express in your cleaned sample? The primary one I can think of is collections β†’ ItemsSource Bindings, but there may be something related to the property to which you assign the associated value (since it is clearly not an object) or something else entirely.

+3
source

You do not notify about changes in the PropertyOne, so the user interface will not be updated. Instead, bind to ComplexValueObject and use a value converter to get the value of the property.

 <Object Value="{Binding Path=ComplexValueObject, Converter={StaticResource ComplexValueConverter}, ConverterParameter=PropertyOne}" /> public class ComplexValueConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { ComplexValue cv = value as ComplexValue; string propName = parameter as string; switch (propName) { case "PropertyOne": return cv.PropertyOne; case "PropertyTwo": return cv.PropertyTwo; default: throw new Exception(); } } public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } } 
+1
source

You need INotifyPropertyChanged in your Complex class. The only notification is that if you reassign the entire property in the parent, the properties of this child class should notify to0 if you intend to contact them.

0
source

Source: https://habr.com/ru/post/1312082/


All Articles