Xamarin Forms MVVM-在listview中测试空白的源数据

时间:2018-09-09 00:34:35

标签: listview xamarin mvvm xamarin.forms

我修改了一些示例代码来显示列表视图,并使其出于我的目的而工作。但是,某些源数据是空白的,会生成空白行,例如,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;
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

我建议向标签的IsVisible属性添加一个新的绑定,然后使用value converter切换可见性,例如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();
    }
}