Xamarin.Forms Listview有2行标签?

时间:2014-12-23 22:24:17

标签: xamarin xamarin.forms monotouch.dialog

我将 MonoTouch.Dialog 应用转换为 Xamarin.Forms

我在ListView中有一个Cell,它有一个" Detail"应该是2行长的行,并且应该在此之后截断尾部。

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

如何设置类似的内容," Lines = 2"

3 个答案:

答案 0 :(得分:1)

我用自定义渲染器解决了这个问题

这是我的xaml(在我的pcl项目中)

<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>

这是我的MultiLineLabel班级

public class MultiLineLabel : Label
    {
    }

这是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 :(得分:0)

以下代码可以正常使用:)

 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
                }
            };

答案 2 :(得分:-3)

您可能想要自定义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>

即使文字不适合一行,它也会自动换行。