How to separate two items by line in Xamarin Forms MenuItems like this pic:
I mean, in the same menu items, I want to draw a horizontal line between two groups of items (not between every item and the other, which done by SeparatorVisibility="Default"
).
Here is cs code:
using System;
using Xamarin.Forms;
namespace GMG
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class MasterDetailPage1Master : ContentPage
{
public ListView ListView;
public MasterDetailPage1Master()
{
InitializeComponent();
BindingContext = new MasterDetailPage1MasterViewModel();
ListView = MenuItemsListView;
}
class MasterDetailPage1MasterViewModel : INotifyPropertyChanged
{
public ObservableCollection<MasterDetailPage1MenuItem> MenuItems { get; set; }
public MasterDetailPage1MasterViewModel()
{
MenuItems = new ObservableCollection<MasterDetailPage1MenuItem>(new[]
{
new MasterDetailPage1MenuItem { Id = 0, Title = "Hospitals", Icon="hosp.png", TargetType= typeof(Hospitals) },
new MasterDetailPage1MenuItem { Id = 1, Title = "Clinics", Icon="cli.png",TargetType= typeof(Clinics) },
new MasterDetailPage1MenuItem { Id = 2, Title = "Pharmacies", Icon="pha.png", TargetType= typeof(Pharma) },
new MasterDetailPage1MenuItem { Id = 3, Title = "Labs", Icon="lab2.png", TargetType= typeof(Labs) },
new MasterDetailPage1MenuItem { Id = 4, Title = "MainPage", Icon="home.png", TargetType= typeof(MasterDetailPage1Detail) },
new MasterDetailPage1MenuItem { Id = 5, Title = "Call us", Icon="cont.png" },
new MasterDetailPage1MenuItem { Id = 6, Title = "Rating App", Icon="rate.png" },
});
}
#region INotifyPropertyChanged Implementation
public event PropertyChangedEventHandler PropertyChanged;
void OnPropertyChanged([CallerMemberName] string propertyName = ""){
if (PropertyChanged == null)
return;
PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName));
}}}}
This is Xaml Code:
<StackLayout>
<StackLayout>
<Label Text="GMG"/>
</StackLayout>
<ListView x:Name="MenuItemsListView"
RowHeight="55"
ItemsSource="{Binding MenuItems}">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout>
<Label Text="{Binding Title}"/>
<Image Source="{Binding Icon}"/>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
</ContentPage>
To create a line you just have to add BoxView graphic control and assign height or width value that you want for your line in the HeightRequest/WidthRequest attributes.
You can add a separator for the whole row by customizing the SfTreeView expander and indentation in Xamarin. Forms. Set ExpanderWidth and Indentation to zero to disable the default expander and indentation.
To draw a line, create a Line object and set its X1 and Y1 properties to its start point, and its X2 and Y properties to its end point. In addition, set its Stroke property to a Brush -derived object because a line without a stroke is invisible. For more information about Brush objects, see Xamarin. Forms Brushes.
The Margin and Padding properties are both of type Thickness . There are three possibilities when creating a Thickness structure: Create a Thickness structure defined by a single uniform value. The single value is applied to the left, top, right, and bottom sides of the element.
Your XAML code will be something like this
<StackLayout>
<StackLayout>
<Label Text="GMG"/>
</StackLayout>
<ListView x:Name="MenuItemsListView"
RowHeight="55"
ItemsSource="{Binding MenuItems}">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout Orientation="Vertical">
<StackLayout Orientation="Horizontal">
<Label Text="{Binding Title}"/>
<Image Source="{Binding Icon}"/>
</StackLayout>
<BoxView HeightRequest="1" Color="#00000" IsVisible="{Binding IsSeparatorVisible}"/>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
And in your code it should be like set the IsSeparatorVisible to true/false for showing/hiding the line
new MasterDetailPage1MenuItem { Id = 0, Title = "Hospitals", Icon="hosp.png",IsSeparatorVisible = true, TargetType= typeof(Hospitals) },
new MasterDetailPage1MenuItem { Id = 1, Title = "Clinics", Icon="cli.png",IsSeparatorVisible = false,TargetType= typeof(Clinics) },
I got here searching, "How to put a horizontal line in my XAML code?" If that's all you want, just put this in:
<BoxView HeightRequest="1" Color="LightGray" Margin ="40, 0, 40, 0"/>
Sorry my bad, you have to put a BoxView inside the listview and set its visibility e.g.
<BoxView HeightRequest="1" Color="#00000" IsVisible="{Binding IsSeparatorVisible"/>
and make it true when for example your title reached e.g. Inbox. You can do this by looping through the collection you have created and set it there.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With