C#C
C#4mo ago
Zoli

View or ViewModel duty?

I am building a Maui application where the user can switch between using "kg" or "lbs" across the application.
So the main dilemma I have a Weight property and how to update if the preferred unit has been changed by the user the two ways to update:
Some extra information
I store the weight in kg in the database
there are many collectionviews, also tabbed view so singleton

View with unit converter

For this I could just use MultiBinding to pass the currenct weight value and if currently UseImperialUnits state, ApplicationState is added to the App constructor as resource.

<Label>
    <Label.FormattedText>
        <FormattedString>
            <Span>
                <Span.Text>
                    <MultiBinding Converter="{StaticResource WeightWithUnitConverter}">
                        <Binding Path="Weight" />
                        <Binding Path="UseImperialUnits" Source="{StaticResource ApplicationState}" />
                    </MultiBinding>
                </Span.Text>
            </Span>
        </FormattedString>
    </Label.FormattedText>
</Label>



This works so if user switchs the weight unit it updates the ui automatically and calculates the new value for only displaying underhood the Weight property is not changed.

ViewModel

I have a getter property on the ViewModel:
public double Weight => _applicationState.UseImperialUnits ? _model.Weight.ToPounds() : _model.Weight;

In this case for sure I would need to subscribe to the PropertyChange of the UseImperialUnits.ApplicationState so if that changes we need to call:
OnPropertyChanged(nameof(Weight ))
this means in collection I would need to do with all items.


Which approach do you consider best practice in MVVM for handling global unit system preferences?
Do you stick with converters for UI-only scenarios, or do you prefer propagating PropertyChanged through all affected VMs? Any recommended patterns to make this scalable and avoid memory leaks?
Was this page helpful?