listview - Xamarin Forms MVVM - 测试列表视图中空白的源数据
问题描述
我修改了一些示例代码来显示一个列表视图并让它为我的目的工作。然而,一些源数据是空白的,这会产生一个空白行,例如,Spouse2 通常是空白的。我想测试空白而不生成该行。具体来说,如何
s2Label.SetBinding(Label.TextProperty, new Binding("Spouse2"));
在 CustomTreeBranches 类中测试该语句检索到的源数据以绕过该
vlayout.Children.Add(s2Label);
语句?
以下是课程:
public partial class MainView : ContentPage
{
public ObservableCollection<Models.TreeBranches> branches { get; set; }
public MainView()
{
InitializeComponent();
branches = new ObservableCollection<Models.TreeBranches>();
ListView viewList = new ListView();
this.Title = "Family Tree";
viewList.ItemTemplate = new DataTemplate(typeof(CustomTreeBranches));
branches.Add(new Models.TreeBranches {
BranchNumber = 1,
FullName = "Melvin Sneerd",
Birthday ="1/22/1982",
Father = "Dick Sneerd",
Mother = "Hazel Haze",
Spouse1 = "Lucky Lady",
Spouse2 = "",
Spouse3 = "",
Child1 = "John Sneerd",
Child2 = "Donna Sneerd",
Child3 = "",
Child4 = "",
Residence = "Malibu, California",
Image = "Photos/filler.jpg" });
branches.Add(new Models.TreeBranches {
BranchNumber = 2,
FullName = "Lucky Lady",
Birthday ="11/31/1980",
Father = "Papa Joe",
Mother = "Mama Bear",
Spouse1 = "Melvin Sneerd",
Spouse2 = "",
Spouse3 = "",
Child1 = "John Sneerd",
Child2 = "Donna Sneerd",
Child3 = "",
Child4 = "",
Residence = "Highlands Ranch, Colorado",
Image = "Photos/fill.jpg" })
public class CustomTreeBranches : ViewCell
{
public CustomTreeBranches()
{
var img = new Image();
var bdLabel = new Label();
var nameLabel = new Label();
var bnLabel = new Label();
var frLabel = new Label();
var mrLabel = new Label();
var c1Label = new Label();
var c2Label = new Label();
var c3Label = new Label();
var c4Label = new Label();
var s1Label = new Label();
var s2Label = new Label();
var s3Label = new Label();
var rdLabel = new Label();
var sp0Label = new Label()
{
Text = "Branch Member",
FontAttributes = FontAttributes.Bold,
HorizontalOptions = LayoutOptions.Center
};
var sp1Label = new Label()
{
Text = "Father and Mother",
FontAttributes = FontAttributes.Bold,
HorizontalOptions = LayoutOptions.Center
};
var sp2Label = new Label()
{
Text = "Children",
FontAttributes = FontAttributes.Bold,
HorizontalOptions = LayoutOptions.Center
};
var sp3Label = new Label()
{
Text = "Spouse(s)",
FontAttributes = FontAttributes.Bold,
HorizontalOptions = LayoutOptions.Center
};
var sp4Label = new Label()
{
Text = "Latest Residence",
FontAttributes = FontAttributes.Bold,
HorizontalOptions = LayoutOptions.Center
};
var vLayout = new StackLayout();
var hLayout = new StackLayout() { BackgroundColor = Color.LavenderBlush };
img.SetBinding(Image.SourceProperty, new Binding("Image"));
bdLabel.SetBinding(Label.TextProperty, new Binding("Birthday"));
nameLabel.SetBinding(Label.TextProperty, new Binding("FullName"));
//bnLabel.SetBinding(Label.TextProperty, new Binding("BranchNumber"));
frLabel.SetBinding(Label.TextProperty, new Binding("Father"));
mrLabel.SetBinding(Label.TextProperty, new Binding("Mother"));
c1Label.SetBinding(Label.TextProperty, new Binding("Child1"));
c2Label.SetBinding(Label.TextProperty, new Binding("Child2"));
c3Label.SetBinding(Label.TextProperty, new Binding("Child3"));
c4Label.SetBinding(Label.TextProperty, new Binding("Child4"));
s1Label.SetBinding(Label.TextProperty, new Binding("Spouse1"));
s2Label.SetBinding(Label.TextProperty, new Binding("Spouse2"));
s3Label.SetBinding(Label.TextProperty, new Binding("Spouse3"));
rdLabel.SetBinding(Label.TextProperty, new Binding("Residence"));
hLayout.Orientation = StackOrientation.Horizontal;
hLayout.HorizontalOptions = LayoutOptions.Fill;
img.HorizontalOptions = LayoutOptions.End;
img.WidthRequest = 250;
img.HeightRequest = 250;
nameLabel.FontSize = 18;
vLayout.Children.Add(sp0Label);
vLayout.Children.Add(nameLabel);
vLayout.Children.Add(bdLabel);
//vLayout.Children.Add(bnLabel);
vLayout.Children.Add(sp1Label);
vLayout.Children.Add(frLabel);
vLayout.Children.Add(mrLabel);
vLayout.Children.Add(sp2Label);
vLayout.Children.Add(c1Label);
vLayout.Children.Add(c2Label);
vLayout.Children.Add(c3Label);
vLayout.Children.Add(c4Label);
vLayout.Children.Add(sp3Label);
vLayout.Children.Add(s1Label);
vLayout.Children.Add(s2Label);
vLayout.Children.Add(s3Label);
vLayout.Children.Add(sp4Label);
vLayout.Children.Add(rdLabel);
hLayout.Children.Add(vLayout);
hLayout.Children.Add(img);
View = hLayout;
}
}
}
}
解决方案
我建议将新绑定添加到标签的 IsVisible 属性,然后使用值转换器来切换可见性,就像s2Label.SetBinding(IsVisibleProperty, "Spouse2", BindingMode.Default, new MyValueConverter());
您的值转换器可能看起来像
public class MyValueConverter: IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var myString = value as string;
return (myString == null) ? false : !string.IsNullOrWhiteSpace(myString);
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
推荐阅读
- java - 无法在android java中实现自定义日历视图库
- python - 如何将字符串变量从一个函数传递到另一个函数?
- sql - 如何在sqlite中将未知数量的表合并在一起
- python - How to end a for loop after a given amount of time
- c++ - 使用 static_cast 更改派生结构成员
- ocaml - How to send multiple TCP messages and continue when one has succeeded
- python - 发生异常后如何返回循环?
- javascript - Angular 8 使用带有异步管道的 ngIf 来显示和隐藏 HTML 元素
- python-3.x - Python dataframe with value 'NA' not fetching
- reactjs - Access to outer react component from referenced component