【发布时间】:2020-12-01 06:01:13
【问题描述】:
我有一个要绑定到布尔属性 (HasNotifications) 的标签。但是,当属性为 false 时,标签保持可见。如果我在 XAML 中将 IsVisible 属性设置为 false,则标签不可见,因此问题似乎与绑定有关。
XAML:
<AbsoluteLayout
HorizontalOptions="FillAndExpand"
VerticalOptions="FillAndExpand">
<Grid
HorizontalOptions="FillAndExpand"
VerticalOptions="FillAndExpand"
AbsoluteLayout.LayoutFlags="All"
AbsoluteLayout.LayoutBounds="0,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);
}
【问题讨论】:
-
您确定您的 BindingContext 设置正确吗?你的虚拟机是否实现了 INotifyPropertyChanged?
-
@Jason 我相信是的。 viewmodel 还包含一个 ObservableCollection,我将它绑定到 ListView 的 ItemsSource 的属性并且看起来没问题。
-
为什么必须为两个标签指定单独的 BindingContext?您可以尝试将
HasNotifications绑定到 Label 的 Text 属性以验证它是否按预期工作?
标签: xamarin.forms