简体   繁体   中英

Synchronize the IsEnabled property of a control with another boolean property

I am building a Xamarin application and I have a Content Page which has a Picker and a Switch.

The Picker shall be disabled when the IsToggled property of the picker is set to true, and vice versa. So I have bounded the respectives properties involved to the properties of a ViewModel class like so:

//XAML:
<Switch IsToggled="{Binding UseNearestShop}"/>
<Picker Title="Select your favorite shop" ItemsSource="{Binding AvailableShops}" SelectedItem="{Binding FavoriteShop}" IsEnabled="{Binding UseNearestShop, Converter={StaticResource booleanNegation}}"/>

So when the page is initialized, the Picker is correctly toggled or not according to the value of my ViewModel UseNearestShop property (which itself is correctly modified by the toggling of the Picker).

My issue is: how could I make the the IsEnabled property be modified everytime the UseNearestShop value is modified? Do I have to add another property in my ViewModel for it?

Any insight would be appreciated.

<StackLayout>
    <Switch x:Name="MySwitch" />
    <Picker IsEnabled="{Binding IsToggled, Source={x:Reference MySwitch}, Converter={StaticResource booleanNegation}}" />
</StackLayout>

You can even remove exciting property from your view model instead of adding new one!

And I think your current code has a problem because of wrong implementation of INotifyPropertyChanged in your view model.

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