0

I've got several times the question, but never figured out how to properly resolve it.

I've an object, SensorOrientation, which is composed of an float and a boolean:

public class SensorOrientation{
    public float? RadialAngle {
        get{
            //Getting from backingField
        }
        set{
            //Setting the backingField
            //Notifying
        }
    }

    public bool IsAxial {       
        get{
            //Getting from backingField
        }
        set{
            //Setting the backingField
            //Notifying
        }
    }
}

In my ViewModel, I've one SensorOrientation property, and one other property, which is a basic ViewModel(I've a graph with 9 position, and I've to select the one that is the closest to the custom value in the RadialAngle).

So I've created a enum:

public enum OrientationEditorDirection
{
    Unknown,
    Center,
    Top,
    TopRight,
    Right,
    BottomRight,
    Bottom,
    BottomLeft,
    Left,
    TopLeft
}

So in my ViewModel, I want to update this when the value of "RadialAngle" or "IsAxial" has changed:

public class ViewModel{

    public SensorOrientation Orientation {  
        get{
            //Getting from backingField
        }
        set{
            //Setting the backingField
            //Notifying
        }
    }

    public OrientationEditorDirection Direction {
        get=> CallToTheMethodThatComputeTheCorrectDirection();
        set => CallToTheMethodThatUpdateTheSensorOrientationValue(value);
    }
}

In the xaml of this ViewModel, I've 2 differents sub-controls, one that display the direction, one that is a custom field editing the exact value of the ? Orientation.RadialAngle(when the guy clicks on Center(which is equal to Axial), then the custom textbox is hidden).

 <local:OrientationEditor SelectedDirection="{Binding Direction}"  />
 <angleEditor:AngleEditor Angle="{Binding Orientation.Angle }"/>

The issue is that since my AngleEditor is bound(and has to be bound, it is used in some other case), on a sub-sub-property, I don't see when I could raise the property changed for the Direction.

The best I could do as of today, was that in my ViewModel, when I set a new "Orientation", I register on the PropertyChanged event manually, and when something change I update the Direction. But I find that quite ugly not very WPFish.

Any idea how I can do this better?

J4N
  • 19,480
  • 39
  • 187
  • 340
  • 1
    Possible duplicate of [MVVM How to get notified when nested objects properties are updated?](https://stackoverflow.com/questions/52850788/mvvm-how-to-get-notified-when-nested-objects-properties-are-updated) – Peregrine Oct 17 '18 at 11:53
  • @Peregrine Really? I indicate that as of now, this is the only way I've found but I was looking for a better WPFish approach? This has some severe limitation, nobody is gonna to unregister from it, and I've some cases where I'm linked with the sub-sub-sub properties. Also, if it's the onyl behavior, could it be wrapped? Like with attached property, or something like this? – J4N Oct 17 '18 at 12:12
  • I usually handle things like this with events and manager classes. I'd have for example an OrientationManager which would hold the current state of variables in which multiple parts of the application are interested in. I'd then either directly use those values (VM property getter would return Manager.Property instead of local property) or the manager would raise an event when the property changes to which anyone interested can subscribe to and react (even if just to notify the property changed). I'm not 100% this is applicable to your case, but this is how I usually handle simmiliar issues. – HotTowelie Oct 17 '18 at 20:31
  • 1
    You should either handle the PropertyChanged event for the Orientation as you suggest your self in your last paragraph, or you should bind directly to the sub-property. – mm8 Oct 18 '18 at 14:31

0 Answers0