如何在Xamarin表单中使用XAML隐藏TableSection? [英] How do I hide a TableSection with XAML in Xamarin Forms?
问题描述
我最近一直在使用Xamarin Forms进行一个项目,并且我一直在使用TableView来显示从Web api检索到的记录的详细信息.有时,某些细节不存在,所以我想隐藏显示信息的部分.
I have been working in Xamarin Forms lately for a project, and I have been using the TableView to show details of a record retrieved from web api. Sometimes, certain details are not present, so I'd like to hide the section that displays the information.
但是,我找不到隐藏TableSection的方法.
However, I can't find a way to hide the TableSection.
这里是一些XAML:
<TableView>
<TableRoot>
...
<!--Contact info-->
<TableSection IsVisible="{Binding HasContact}" Title="Contact">
<!--Contact name-->
<TextCell Text="{Binding ContactName}" Detail="Primary contact" />
<!--Phone-->
<TextCell Text="Phone"
Detail="{Binding FormattedContactPhoneNumber}"
Command="{Binding BindingContext.DialPhoneCommand, Source={x:Reference MainGrid}}"
CommandParameter="{Binding ContactPhoneNumber}"/>
<!--Email-->
<TextCell Text="Email"
Detail="{Binding ContactEmail}"
Command="{Binding BindingContext.SendEmailCommand, Source={x:Reference MainGrid}}"
CommandParameter="{Binding ContactEmail}"/>
</TableSection>
</TableRoot>
</TableView>
很显然,IsVisible属性不起作用,并因为不存在而引发异常(它存在于其他元素(如Labels)上).我还尝试使用VisualElement.IsVisible
抛出无效的强制转换异常.那么有什么办法可以隐藏此部分?
Obviously, the IsVisible property didn't work and throws an exception because it doesn't exist (It is present on other elements like Labels). I also tried using VisualElement.IsVisible
which throws an invalid cast exception. So is there any way to hide this section?
如果没有办法,也许我需要走一条肮脏的路,并使用单独的TableViews(可以在其中使用VisualElement.IsVisible):(
If there isn't a way to do it, perhaps I'll need to go down a dirtier path and use separate TableViews (There I can use VisualElement.IsVisible) :(
推荐答案
好吧,您遇到了使用TableView的一个缺点,即无法通过可绑定属性动态隐藏节.
Well you've hit the one drawback of using a TableView, not being able to hide sections dynamically through bindable properties.
在我的项目中,我这样解决了这个问题:
In my project I solved this like so:
在页面后面的代码中,我侦听用作BindingContext的ViewModel的OnPropertyChanges. 当所需的布尔值更改时,我将删除TableSection中不再需要的单元格. 当再次需要该单元格时,我再次插入它.
In the code behind of the page I listen for OnPropertyChanges of the ViewModel that is used as the BindingContext. When the needed boolean changes, I Remove the Cell that is no longer needed in the TableSection. When the Cell is needed again, I Insert it again.
因此,请为所有节和单元格命名,并在页面开始时保留需要更改的单元格以供以后删除和添加它们以供参考.
So name all sections and cells and at page start get a hold of those cells that need to change for reference for removing and adding them later.
小示例代码
private void OnViewmodelPropertyChanged (object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
if (e.PropertyName.Equals("IsBioSecurityAvailable", StringComparison.OrdinalIgnoreCase))
{
AdjustBioSecurityHeight();
}
}
private void AdjustBioSecurityHeight()
{
if (!_viewmodel.IsBioSecurityAvailable && GeneralSection.Contains(BioSecurityViewCell))
GeneralSection.Remove(BioSecurityViewCell);
}
这篇关于如何在Xamarin表单中使用XAML隐藏TableSection?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!