Xamarin表单:StackLayouts,ListViews和网格哦我的

时间:2016-12-05 19:05:28

标签: listview mvvm xamarin.forms

我正在努力让我的布局表现得很好。我正在尝试创建一个页面,显示一个人以及他们的孩子列表,让他们更新他们有多少孩子。

这是我目前所拥有的:

<StackLayout Orientation="Vertical" HorizontalOptions="Center">
  <controls:BindablePicker x:Name="pkrChildren" ItemsSource="{Binding ChildrenItems}" Title="Num. Children" SelectedItem="{Binding Path=CurrentPerson.NumChildren, Mode=TwoWay}" HorizontalOptions="Center" />
  <ListView x:Name="lvChildren" ItemsSource="{Binding Path=CurrentPerson.Children}" HorizontalOptions="Center" SeparatorVisibility="None">
    <ListView.ItemTemplate>
      <DataTemplate>
        <ViewCell>
          <ViewCell.View>
            <Grid>
              <Grid.ColumnDefinitions>
                <ColumnDefinition />
                <ColumnDefinition />
              </Grid.ColumnDefinitions>
              <controls:BindablePicker Title="Age" Grid.Row="0" Grid.Column="0" ItemsSource="{Binding Ages}"  SelectedItem="{Binding Path=ChildModel.Age, Mode=TwoWay}" HorizontalOptions="End" />
              <controls:BindablePicker Title="Units" Grid.Row="0" Grid.Column="1" ItemsSource="{Binding AgeUnits}" SelectedItem="{Binding Path=ChildModel.Unit, Mode=TwoWay}" HorizontalOptions="Start" />
            </Grid>
          </ViewCell.View>
        </ViewCell>
      </DataTemplate>
    </ListView.ItemTemplate>
  </ListView>
  <Label Text="More Details Below" FontAttributes="Bold" HorizontalTextAlignment="Center" />
  <ListView VerticalOptions="EndAndExpand" />
</StackLayout>

看起来像这样:

Try 1

我想要的是“下面的更多详细信息”标签(以及后续控件)向上移动到ListView下方。然后,如果用户更改了顶部的数字,ListView应该展开或收缩(例如,如果他们选择“5”,将出现三个新的空行,一切都应该向下移动。)

我希望这很清楚。我正在使用MVVM,当我更新我的子列表时,ListView会添加和删除行,但它下面的控件不会移动;它几乎就像页面本身需要重新渲染一样。

我尝试在最后添加ListView以扩展以尝试推动所有事情,但这似乎没有帮助。

我可能会在网格中包含lvChildren,但是当我在列表中添加或删除子项时,我需要一种方法让Grid缩小和扩展。

非常感谢任何指导。谢谢你的时间。

更新:这是我的ViewModels

namespace ShortGameTracker.ViewModels
{
    [ImplementPropertyChanged]
    public class PeopleEntryViewModel
    {
        public IList<string> ChildrenItems { get; private set; }
        public CompanyModel CompanyModel { get; set; }
        public ChildEntryViewModel CurrentPerson { get; set; }

        public PeopleEntryViewModel()
        {
            Initialize();
        }

        public PeopleEntryViewModel(object param)
        {
            Initialize();

            var company = param as CompanyModel;
            CompanyModel = company;

            // We need at least one hole to load the view.
            if (CompanyModel.Employees.Count == 0)
            {
                CompanyModel.Employees.Add(new PersonModel());
            }
            CurrentPerson = new ChildEntryViewModel(CompanyModel.Employees[0]);
        }

        private void Initialize()
        {
            ChildrenItems = new List<string> { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };
        }
    }
}

namespace ShortGameTracker.ViewModels
{
    [ImplementPropertyChanged]
    public class ChildEntryViewModel
    {
        private string _NumChildren;
        public string NumChildren
        {
            get
            {
                if (Children == null)
                {
                    return null;
                }
                return Children.Count.ToString();
            }
            set
            {
                if (NumChildren != value)
                {
                    _NumChildren = value;
                    AdjustChildren(Convert.ToInt32(_NumChildren));
                }
            }
        }
        public string ChildNum { get; set; }
        public ObservableCollection<ChildViewModel> Children { get; set; }

        public ChildEntryViewModel()
        {
            Initialize();
        }

        public ChildEntryViewModel(object param)
        {
            var person = param as PersonModel;
            List<ChildViewModel> pvms = new List<ChildViewModel>();
            foreach (ChildModel childModel in person.Children)
            {
                pvms.Add(new ChildViewModel(childModel));
            }
            Children = new ObservableCollection<ChildViewModel>(pvms);
        }

        private void Initialize()
        {
        }

        private void AdjustChildren(int newNumChildren)
        {
            int numToCreate = newNumChildren - Children.Count;
            for (int i = 0; i < numToCreate; i++)
            {
                Children.Add(new ChildViewModel());
            }

            int numToRemove = Children.Count - newNumChildren;
            int idxToRemove = Children.Count - 1;
            for (int i = 0; i < numToRemove; i++)
            {
                Children.RemoveAt(idxToRemove--);
            }
        }
    }
}

namespace ShortGameTracker.ViewModels
{
    [ImplementPropertyChanged]
    public class ChildViewModel
    {
        public IList<string> Ages { get; private set; }
        public ObservableCollection<string> AgeUnits { get; private set; }
        public ChildModel ChildModel { get; set; }

        public ChildViewModel()
        {
            Initialize();
        }
        public ChildViewModel(object param)
        {
            Initialize();

            var childModel = param as ChildModel;
            ChildModel = childModel;
        }
        private void Initialize()
        {
            Ages = new List<string>();
            for (int i = 1; i < 125; i++)
            {
                Ages.Add(i.ToString());
            }
            AgeUnits = new ObservableCollection<string>(new List<string> { "Years", "Months", "Days" });
        }
    }
}

0 个答案:

没有答案