Binding between elements

AdhamAliAdhamAli USMember ✭✭

In terms of best practises, what is the best way for interacting among elements through data binding. Is it setting a x:Name for the source and do a typical data binding. Or it's better to have the source send it's value to the view model using OneWayToSource binding mode and then the target retrieve it from the view model

Answers

  • JohnHardmanJohnHardman GBUniversity mod
    edited August 2018

    @AdhamAli - Can you think of a scenario where some UI interaction by the user would result in something else on the UI updating, but where the interaction does not result in data being updated for use elsewhere? If you can, then that scenario might be one where you don't update the View Model. In other scenarios, it's (IMHO) going to be more reliable to go via the View Model.

    Taking an example such as defining a color using three sliders (one for Red, one for Green, one for Blue), where moving a slider results in an integer value being updated on screen (as a Label's Text property) to show the new value of the slider (so there are three sliders and three integer values), some people might bind the Label to the Slider. However, the R, G and B values all need to be combined to create the resulting Color, presumably for use elsewhere within the app. For that to happen, the values will need to go to the View Model. So, you could have the Slider bound to the View Model and the Label bound to the Slider. IMHO, as the View Model is the "master" of the data (in the absence of a Model for purpose of discussion), it's better to have the Slider bind to the View Model and then the Label also bind to the View Model, so that the Label is updated from the "master". Does it technically make a difference - not of any significance, no. However, from a viewpoint of the "master" being the source of changes to the Label and to the Color used elsewhere in the system, that gives a greater feeling of confidence in the system working as it should, and making extending the system easier (so reducing maintenance effort/cost).

Sign In or Register to comment.