简体   繁体   中英

View updating ViewModel properties at startup

I have a Slider control with minimum value higher than 0. The int type property in the VM that the slider's bound to therefore has a initial value outside of slider value range. How can I get the slider to update the VM property with its miminum value at application startup? Currently it's only updating once the operator uses the slider.

<Slider Grid.Column="2" Grid.Row="1" Minimum="2" Maximum="100"
        AutoToolTipPlacement="BottomRight" AutoToolTipPrecision="0"
        Value="{Binding Path=Controller.MotorModel.SelectedMovementSpeed}" />

EDIT:

My initial assumption that the View should define the operator's limits was wrong. As FCin wrote, the View should only display values, while the logic should be in the ViewModel. Here's my updated XAML:

<Slider Grid.Column="2" Grid.Row="1" AutoToolTipPlacement="BottomRight" AutoToolTipPrecision="0"
        Minimum="{Binding Path=Controller.MotorModel.MinimumMovementSpeed}" 
        Maximum="{Binding Path=Controller.MotorModel.MaximumMovementSpeed}"
        Value="{Binding Path=Controller.MotorModel.SelectedMovementSpeed, Mode=TwoWay}" />

Set your Minimum value inside your ViewModel, not in the View. Views in MVVM should not contain any logic. They only display properties to a user. ViewModels should validate user's input based on bounded properties and methods.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM