Xamarin ListView更新单元格显示

时间:2018-04-18 08:05:25

标签: c# listview xamarin cell

我使用滑块更改单元格的大小,但listView的单元格没有更新 怎么做?还缺少什么?

也不会显示添加新项目,但如果您使用调试器检查它们,列表中会有这样的项目

private void Slider_ValueChanged(object sender, ValueChangedEventArgs e)
{
    lstView.RowHeight = (int)e.NewValue;
}

这项工作的完整代码:

public class RootPage : ContentPage
    {
        ListView listView;
        public RootPage ()
        {
            listView = new ListView() { HorizontalOptions = LayoutOptions.StartAndExpand };
            listView.ItemTemplate = new DataTemplate(typeof(MyCustomCell));
            listView.ItemsSource = new CustomText[] { new CustomText("asd", "bsd"), new CustomText("abra", "kadabra") };
            listView.HasUnevenRows = true;
            var slider = new Slider() {
                VerticalOptions = LayoutOptions.EndAndExpand,
                Maximum = 300,
                Minimum = 100
            };
            slider.ValueChanged += Slider_ValueChanged;
            Content = new StackLayout {
                Spacing = 20,
                Children = {
                    listView,
                    slider
                }
            };
        }

        private void Slider_ValueChanged(object sender, ValueChangedEventArgs e)
        {
            listView.RowHeight = (int)e.NewValue;
        }
    }



public class CustomText
    {
        public string T1 { get; set; }
        public string T2 { get; set; }
        public CustomText(string t1, string t2)
        {
            T1 = t1;
            T2 = t2;
        }
    }
    public class MyCustomCell : ViewCell
    {
        public MyCustomCell()
        {
            var label1 = new Label();
            var label2 = new Label();

            label1.SetBinding(Label.TextProperty, new Binding("T1"));
            label2.SetBinding(Label.TextProperty, new Binding("T2"));

            var horizontalLayout = new StackLayout()
            {
                BackgroundColor = Color.Olive,
                Children = {
                    label1,
                    label2
                }
            };

            View = horizontalLayout;
        }
    }

1 个答案:

答案 0 :(得分:0)

此行:

            listView.HasUnevenRows = true;

完全意味着更改列表视图的RowHeight无效,因为为每行而不是列表视图定义了高度。