Xamarin.Forms Listview with 2 line label?

I am converting a MonoTouch.Dialog application to Xamarin.Forms .

I have a cell in the ListView, and it has a row "Detail", the length of which should be 2 lines long and then trim the tail.

        var lblDetail = new Label
        {
            LineBreakMode = LineBreakMode.TailTruncation,
            Text = "a really long string that should show 2 lines then ..."
        };

How to set something like "Lines = 2"

+4
source share
3 answers

You can customize the ListView:

    <ListView x:Name="ListMenu" ItemsSource="{Binding _YourItems_}" >
      <ListView.ItemTemplate>
        <DataTemplate>
          <ViewCell>
            <ViewCell.View>
              <StackLayout Orientation="Horizontal">
                <Label Text="{Binding _YourText_}" TextColor="Black" />
              </StackLayout>
            </ViewCell.View>
          </ViewCell>
        </DataTemplate>
      </ListView.ItemTemplate>
    </ListView>

even if the text does not match one line, it will be automatically completed.

-3
source

I solved it with special rendering

This is my xaml (in my pcl project)

<ListView ItemsSource="{Binding Cards}">
  <ListView.ItemTemplate>
    <DataTemplate>
      <ViewCell>
          <Grid Padding="10">
            <Grid.ColumnDefinitions>
              <ColumnDefinition Width="60"/>
              <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>
            <Image Source="{Binding Icon}"/>
            <customRenderers:MultiLineLabel Text="{Binding Summary}"
                                            Grid.Column="1"/>
          </Grid>
      </ViewCell>
    </DataTemplate>
  </ListView.ItemTemplate>

</ListView>

This is my class MultiLineLabel

public class MultiLineLabel : Label
    {
    }

This is a renderer for iOS:

[assembly: ExportRenderer(typeof(MultiLineLabel), typeof(MultiLineLabelRenderer))]
namespace NameSpace.iOS.Renderers
{
    public class MultiLineLabelRenderer : LabelRenderer
    {
        protected override void OnElementChanged(ElementChangedEventArgs<Label> e)
        {
            base.OnElementChanged(e);
            if (Control != null)
            {
                Control.LineBreakMode = UILineBreakMode.TailTruncation;
                Control.Lines = 3;
            }
        }
    }
}
+1

:)

 UserNotesListView = new ListView() {
                HorizontalOptions = LayoutOptions.FillAndExpand,
                VerticalOptions = LayoutOptions.FillAndExpand,
                HasUnevenRows=true,
                ItemsSource=//upon your bussiess
            };

            DataTemplate dt=new DataTemplate(()=>
                {
                    var LabelText = new Label();
                    LabelText.SetBinding(Label.TextProperty, new Binding("note"));
                    return new ViewCell { View = LabelText };
            });
            UserNotesListView.ItemTemplate = dt;
            Content = new StackLayout
            {
                HorizontalOptions=LayoutOptions.FillAndExpand,
                Children=
                {
                    UserNotesListView
                }
            };
0

Source: https://habr.com/ru/post/1568825/


All Articles