点击

时间:2015-06-12 20:33:57

标签: xamarin xamarin.forms

我正在尝试实施一个解决方案,以便在使用ListView(以及自定义渲染器,如果需要)进行挖掘时增加Cell Xamarin Forms的大小。

我对C#还是很陌生,对我来说数据绑定的想法还有点不清楚,但是,这似乎是解决这个问题的方法(也许就像绑定这个问题一样)单元格的Height / HeightRequest属性?)。

到目前为止我的尝试都没有成功。

如果有人能给我一个正确的方向,那将非常感激。

谢谢!

2 个答案:

答案 0 :(得分:6)

ViewCell Height

中将BindableProperty公开为Xamarin.Forms 1.4.2x

但是,如果您在BindableProperty中创建自己的Model,则可以实现更改高度,如下所示: -

<强>模型: -

public class MenuItem2 : BindableObject
{

    public static readonly BindableProperty TextProperty = BindableProperty.Create<MenuItem2, string>(p => p.Text, default(string));
    public static readonly BindableProperty CellHeightProperty = BindableProperty.Create<MenuItem2, int>(p => p.CellHeight, default(int));


    public string Text
    {
        get { return (string)GetValue(TextProperty); }
        set { SetValue(TextProperty, value); }
    }

    public int CellHeight
    {
        get { return (int)GetValue(CellHeightProperty); }
        set { SetValue(CellHeightProperty, value); }
    }
}

<强> XAML: -

<StackLayout>

    <Button x:Name="cmdButton1" Text="Change Cell Heights" Clicked="cmdButton1_Clicked"/>

    <ListView x:Name="lstItems" />

</StackLayout>

XAML代码隐藏: -

    lstItems.HasUnevenRows = true;
    lstItems.ItemTemplate = new DataTemplate(typeof(Classes.MenuCell2));
    //
    lstItems.ItemsSource = new List<MenuItem2> 
    {
        new MenuItem2(),
        new MenuItem2(),
        new MenuItem2(),
        new MenuItem2(),
    };

如果您未设置.HasUnevenRows,则可以更改单元格高度。

    void cmdButton1_Clicked(object sender, EventArgs e)
    {
        Random objRandom = new Random();            
        //
        var objItems = lstItems.ItemsSource;
        //
        foreach (MenuItem2 objMenuItem in objItems)
        {
            int intNewCellHeight = objRandom.Next(80, 160);
            objMenuItem.CellHeight = intNewCellHeight;
            objMenuItem.Text = "Cell Height = " + intNewCellHeight.ToString();
        }
    }

自定义ViewCell: -

public class MenuCell2 : ViewCell
{
    public MenuCell2()
    {
        Label objLabel = new Label
        {
            YAlign = TextAlignment.Center,
            TextColor = Color.Yellow,                
        };
        objLabel.SetBinding(Label.TextProperty, new Binding("Text"));


        StackLayout objLayout = new StackLayout
        {
            Padding = new Thickness(20, 0, 0, 0),
            Orientation = StackOrientation.Horizontal,
            HorizontalOptions = LayoutOptions.StartAndExpand,
            Children = { objLabel }
        };

        Frame objFrame_Inner = new Frame
        {
            Padding = new Thickness(15, 15, 15, 15),
            HeightRequest = 36,
            OutlineColor = Color.Accent,
            BackgroundColor = Color.Blue,
            Content = objLayout,                
        };

        Frame objFrame_Outer = new Frame
        {
            Padding = new Thickness(0, 0, 0, 10),
            Content = objFrame_Inner
        };

        View = objFrame_Outer;            

        this.BindingContextChanged += MenuCell2_BindingContextChanged;
    }

    void MenuCell2_BindingContextChanged(object sender, EventArgs e)
    {
        MenuItem2 objMenuItem = (MenuItem2)this.BindingContext;
        objMenuItem.PropertyChanged += objMenuItem_PropertyChanged;
    }

    void objMenuItem_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
    {
        switch (e.PropertyName)
        {
            case "CellHeight":
                this.Height = (this.BindingContext as MenuItem2).CellHeight;
                (this.View as Frame).ForceLayout();
                break;
        }
    }

请务必在ForceLayout ViewCell属性的根元素上调用View,以便正确重绘。

这将为您提供类似于以下内容的结果(仅在WindowsPhone上测试): -

List with custom cell heights

为了在被点击的ViewCell上进行操作,请在XAML页面上添加: -

    lstItems.ItemTapped += lstItems_ItemTapped;

然后将项目的model更改为以下内容: -

    void lstItems_ItemTapped(object sender, ItemTappedEventArgs e)
    {
        (e.Item as MenuItem2).CellHeight = 200;
    }

答案 1 :(得分:3)