我在XAML页面上有一个开关和标签:
<Switch x:Name="CpSwitch" Toggled="CpSwitch" />
<ViewCell x:Name="aBtn" Tapped="openPicker">
<Grid VerticalOptions="CenterAndExpand" Padding="20, 0">
<Label Text="Don't Know" />
<Picker x:Name="aBtnPicker" SelectedIndexChanged="aBtnPickerSelectedIndexChanged" ItemsSource="{Binding Points}">
</Picker>
<Label Text="{Binding ABtnLabel}"/>
</Grid>
</ViewCell>
我的ViewModel
public class SettingsViewModel: ObservableProperty
{
public SettingsViewModel()
{
}
我想做的是使用视图模型,在该视图模型中,切换状态可以使ViewCell可见。
有人可以给我一些建议我如何使用ViewModel绑定开关和可见属性。
基本上您将它们都绑定到视图模型中的同一Boolean
变量。
<Switch IsToggled="{Binding ShowViewCell}" />
<ViewCell IsVisible="{Binding ShowViewCell}">
...
</ViewCell>
在您的视图模型中:
public class SettingsViewModel
{
public bool ShowViewCell {get;set;}
}
这可以确保开关打开时,视频可见。如果您想实现相反的目标,则可以编写一个自定义转换器:
public class InverseBoolConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return !(bool)value;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return !(bool)value;
}
}
将其添加到您的app.xaml资源词典:
<Application.Resources>
<ResourceDictionary>
<converters:InverseBoolConverter x:Key="InverseBoolConverter" />
</ResourceDictionary>
</Application.Resources>
并将其应用于ViewCell:
<Switch IsToggled="{Binding ShowViewCell, Converter={StaticResource InverseBoolConverter}}" />