在Xamarin Forms中将IsVisible绑定到bool属性

问题描述

我有一个要绑定到布尔属性(HasNotifications)的标签。但是,当属性为false时,标签保持可见。如果我在XAML中将IsVisible属性设置为false,则标签不可见,因此问题似乎出在绑定上。

XAML:

<AbsoluteLayout
    HorizontalOptions="FillAndExpand"
    VerticalOptions="FillAndExpand">

    <Grid
        HorizontalOptions="FillAndExpand"
        VerticalOptions="FillAndExpand"
        AbsoluteLayout.LayoutFlags="All"
        AbsoluteLayout.LayoutBounds="0,1,1">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>

        <Label 
            Text="Title" 
            HorizontalOptions="Center"
            VerticalOptions="Center"
            TextColor="White"
            FontSize="Large"
            FontAttributes="Bold"
            Margin="5"
            BindingContext="{x:Reference DashboardPageView}"
            Grid.Row="0" />

        <Label 
            Text="Notifications" 
            HorizontalOptions="Start"
            VerticalOptions="Center"
            TextColor="White"
            FontSize="Medium"
            FontAttributes="Bold"
            Margin="3"
            BindingContext="{x:Reference DashboardPageView}"
            IsVisible="{Binding HasNotifications}"
            Grid.Row="1" />
    </Grid>
</AbsoluteLayout>

我的视图模型:

public bool HasNotifications
{
    get => this.hasNotifications;
    set => this.SetProperty(ref this.hasNotifications,value);
}

解决方法

我认为您没有设置正确的BindingContextHasNotificationsViewModel的属性,而您设置为标签的BindingContext是DashboardPageView。

我编写了一个简单的演示,希望您能从中获得一些想法:

在xaml中:

<Grid
HorizontalOptions="FillAndExpand"
VerticalOptions="FillAndExpand"
AbsoluteLayout.LayoutFlags="All"
AbsoluteLayout.LayoutBounds="0,1,1">
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        <RowDefinition Height="Auto" />
        <RowDefinition Height="*" />
        <RowDefinition Height="Auto" />
        <RowDefinition Height="*" />
    </Grid.RowDefinitions>

    <Label 
    Text="Title" 
    HorizontalOptions="Center"
    VerticalOptions="Center"
    TextColor="Black"
    FontSize="Large"
    FontAttributes="Bold"
    Margin="5"
    Grid.Row="0" />

    <Label 
    Text="Notifications" 
    HorizontalOptions="Start"
    VerticalOptions="Center"
    TextColor="Black"
    FontSize="Medium"
    FontAttributes="Bold"
    Margin="3"
    IsVisible="{Binding HasNotifications}"
    Grid.Row="1" />

    <Button Text="change HasNotifications" Clicked="Button_Clicked" Grid.Row="2"/>
    
</Grid>

在CS中:

public partial class MainPage : ContentPage
{

    ViewModel myViewModel;
    public MainPage()
    {
        InitializeComponent();

        myViewModel = new ViewModel();

        BindingContext = myViewModel;
    }

    private void Button_Clicked(object sender,EventArgs e)
    {
        myViewModel.HasNotifications = !myViewModel.HasNotifications;
    }
}

public class ViewModel : INotifyPropertyChanged
{
    bool _HasNotifications;

    public event PropertyChangedEventHandler PropertyChanged;

    public ViewModel()
    {

    }

    public bool HasNotifications
    {
        set
        {
            if (_HasNotifications != value)
            {
                _HasNotifications = value;

                if (PropertyChanged != null)
                {
                    PropertyChanged(this,new PropertyChangedEventArgs("HasNotifications"));
                }
            }
        }
        get
        {
            return _HasNotifications;
        }
    }
}

如果有任何问题,随时问我。

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...