|
<?xml version="1.0" encoding="utf-8" ?>
xmlns:local="clr-namespace:ListViewXamarin"
xmlns:sync="clr-namespace:Syncfusion.ListView.XForms;assembly=Syncfusion.SfListView.XForms"
x:Class="ListViewXamarin.MainPage">
<ContentPage.BindingContext>
<local:ViewModel />
</ContentPage.BindingContext>
<ContentPage.Resources>
<ResourceDictionary>
<local:VisibilityConverter x:Key="VisibilityConverter"/>
</ResourceDictionary>
</ContentPage.Resources>
<Grid RowSpacing="0" ColumnSpacing="0" Padding="0" Margin="0">
<sync:SfListView x:Name="listView"
AutoFitMode="DynamicHeight"
ItemsSource="{Binding BookInfo}"
ItemSpacing="3"
SelectionBackgroundColor="#d3d3d3">
<sync:SfListView.ItemTemplate>
<DataTemplate>
<Grid BackgroundColor="Beige">
<Grid.Behaviors>
<local:Behavior/>
</Grid.Behaviors>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Label x:Name="label" Text="{Binding BookDescription}" MaxLines="2" Opacity=" 0.54" TextColor="#000000" FontSize="13"/>
<Button x:Name="seeMoreButton" Text="See more..." Grid.Row="1" IsVisible="{Binding LineLength, Converter={StaticResource VisibilityConverter}, ConverterParameter=SeeMore}" BackgroundColor="Transparent"/>
<Button x:Name="seeLessButton" Text="See less..." Grid.Row="1" IsVisible="{Binding LineLength, Converter={StaticResource VisibilityConverter}, ConverterParameter=SeeLess}" BackgroundColor="Transparent"/>
</Grid>
</DataTemplate>
</sync:SfListView.ItemTemplate>
</sync:SfListView>
</Grid>
</ContentPage> |
|
internal ObservableCollection<BookInfo> GetBookInfo()
{
var bookInfo = new ObservableCollection<BookInfo>();
for (int i = 0; i < BookDescriptions.Count(); i++)
{
var book = new BookInfo() { BookDescription = BookDescriptions[i], LineLength = BookDescriptions[i].Length };
bookInfo.Add(book);
}
return bookInfo;
} |
|
public class VisibilityConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if ((int)value > 300)
return parameter.ToString() == "SeeMore" ? true : false;
return false;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
} |
|
public class Behavior : Behavior<Grid>
{
Label Label;
Button SeeMoreButton;
Button SeeLessButton;
protected override void OnAttachedTo(Grid bindable)
{
Label = bindable.FindByName<Label>("label");
SeeMoreButton = bindable.FindByName<Button>("seeMoreButton");
SeeLessButton = bindable.FindByName<Button>("seeLessButton");
SeeMoreButton.Clicked += SeeMoreButton_Clicked;
SeeLessButton.Clicked += SeeLessButton_Clicked;
base.OnAttachedTo(bindable);
}
private void SeeMoreButton_Clicked(object sender, EventArgs e)
{
SeeMoreButton.IsVisible = false;
SeeLessButton.IsVisible = true;
Label.MaxLines = 10;
}
private void SeeLessButton_Clicked(object sender, EventArgs e)
{
SeeLessButton.IsVisible = false;
SeeMoreButton.IsVisible = true;
Label.MaxLines = 2;
}
} |
|
public class Behavior : Behavior<Grid>
{
Label Label;
Button Button;
protected override void OnAttachedTo(Grid bindable)
{
bindable.ChildAdded += Bindable_ChildAdded;
base.OnAttachedTo(bindable);
}
private void Bindable_ChildAdded(object sender, ElementEventArgs e)
{
if (e.Element is Label)
Label = e.Element as Label;
else if(e.Element is Button)
Button = e.Element as Button;
//Also, you can get the element using StyleId 'e.Element.StyleId == "expandButton"'
}
} |