I have a collection view, where each of item is selected with the same brown color, this is my item
this is my code:
<CollectionView SelectionMode="Single" ItemsSource="{Binding CeLLs}">
<CollectionView.ItemTemplate>
<DataTemplate>
<StackLayout>
<Label TextColor="Black" background="Black"/>
i want to change the color brown
In the document, there is a section shows you how to change color of a selected item.
CollectionView has a Selected VisualState that can be used to initiate a visual change to the selected item in the CollectionView:
In your case, change the backgroundColor of stacklayout would work:
<ContentPage.Resources>
<Style TargetType="StackLayout">
<Setter Property="VisualStateManager.VisualStateGroups">
<VisualStateGroupList>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Normal" />
<VisualState x:Name="Selected">
<VisualState.Setters>
<Setter Property="BackgroundColor"
Value="LightSkyBlue" />
</VisualState.Setters>
</VisualState>
</VisualStateGroup>
</VisualStateGroupList>
</Setter>
</Style>
</ContentPage.Resources>
<CollectionView SelectionMode="Single">
<CollectionView.ItemsSource>
<x:Array Type="{x:Type x:String}">
<x:String>Baboon</x:String>
<x:String>Capuchin Monkey</x:String>
<x:String>Blue Monkey</x:String>
<x:String>Squirrel Monkey</x:String>
<x:String>Golden Lion Tamarin</x:String>
<x:String>Howler Monkey</x:String>
<x:String>Japanese Macaque</x:String>
</x:Array>
</CollectionView.ItemsSource>
<CollectionView.ItemTemplate>
<DataTemplate>
<StackLayout>
<Label Text="{Binding .}" TextColor="Black"/>
</StackLayout>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
Another way is binding the BackgroundColor with the property in your ViewModel and change it when the item is selected:
<Label Text="{Binding .}" TextColor="Black" BackgroundColor="{Binding backColor}"/>
Related
I am using the latest xamarin forms version because in the previous version I am facing the issue in the collection view when I selected the item the selected item color set as gray if I set the background as Transparent. I upgraded the new version ( https://github.com/xamarin/Xamarin.Forms/pull/14672 they mentioned it was fixed in the latest version) but I am getting the same issue in the latest version also.
<ContentPage.Resources>
<Style TargetType="Grid">
<Setter Property="VisualStateManager.VisualStateGroups">
<VisualStateGroupList>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Normal" />
<VisualState x:Name="Selected">
<VisualState.Setters>
<Setter Property="BackgroundColor"
Value="Transparent" />
</VisualState.Setters>
</VisualState>
</VisualStateGroup>
</VisualStateGroupList>
</Setter>
</Style>
</ContentPage.Resources>
<StackLayout Margin="20">
<CollectionView ItemsSource="{Binding Monkeys}"
SelectionMode="Single" >
<CollectionView.ItemTemplate>
<DataTemplate>
<Grid Padding="10">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<Image Grid.RowSpan="2"
Source="{Binding ImageUrl}"
Aspect="AspectFill"
HeightRequest="60"
WidthRequest="60" />
<Label Grid.Column="1"
Text="{Binding Name}"
FontAttributes="Bold" />
<Label Grid.Row="1"
Grid.Column="1"
Text="{Binding Location}"
FontAttributes="Italic"
VerticalOptions="End" />
</Grid>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
</StackLayout>
Anyone please suggest any workaround for this. My requirement is to set the background as Transparent for selected Item in xamarin iOS
I have issue with CollectionView and VisualStateManager.
This is my CollectionView. I've also trying to use Compiled Bindings.
<CollectionView Grid.Row="1"
ItemSizingStrategy="MeasureAllItems"
ItemsSource="{Binding SizeOptions}"
Margin="0"
SelectionChangedCommand="{Binding SelectionChangedCommand}"
SelectedItem="{Binding SelectedSizeOption}"
SelectionMode="Single">
<CollectionView.Header>
<BoxView VerticalOptions="Start"
HeightRequest="1"
Color="{StaticResource DividerColor}"/>
</CollectionView.Header>
<CollectionView.ItemTemplate>
<DataTemplate x:DataType="tpViewModels:SizeOption">
<StackLayout IsEnabled="{Binding IsEnabled}">
<StackLayout AutomationId="{Binding Text}"
AutomationProperties.IsInAccessibleTree="True"
HeightRequest="64"
IsEnabled="{Binding IsEnabled}"
Margin="10, 0, 0, 0"
MinimumHeightRequest="64"
Orientation="Horizontal"
Padding="0"
x:Name="Holder">
<Image HeightRequest="20"
IsEnabled="{Binding IsEnabled}"
WidthRequest="20"
x:Name="RadioButtonImage">
</Image>
<Label FontSize="14"
HorizontalOptions="StartAndExpand"
IsEnabled="{Binding IsEnabled}"
Padding="10, 0"
Text="{Binding Text}"
Style="{StaticResource MediumFontFamily}"
VerticalTextAlignment="Center"
x:Name="RadioButtonLabel">
</Label>
</StackLayout>
<BoxView VerticalOptions="Start"
HeightRequest="1"
Color="{StaticResource DividerColor}"/>
</StackLayout>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
When I don't use DataType in DataTemplate, then IsEnabled = false woks perfect, we cannot select item in list. But when I use DataType, then I can select diabled item.
Also I want to change TextColor, FontAttributes and Image.Source when item should be disabled.
It sets for me only VisualStates = Normal or Selected, VisualStateManager doesn't works with State = Disabled.
I've added VisualStateManager in first StackLayout with TargetName setted to x:Name of Label and Image.
DataTrigger for Label and Image works, but I can selected disabled item :(
Anyone knows why?
You could try to set the Visual State Manager to the ContentPage.Resources.
Like:
<ContentPage.Resources>
<Style TargetType="Label">
<Setter Property="VisualStateManager.VisualStateGroups">
<VisualStateGroupList>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Normal" >
<VisualState.Setters>
<Setter Property="TextColor"
Value="LightSkyBlue" />
</VisualState.Setters>
</VisualState>
<VisualState x:Name="Selected">
<VisualState.Setters>
<Setter Property="TextColor"
Value="LightSkyBlue" />
</VisualState.Setters>
</VisualState>
<VisualState x:Name="Disabled">
<VisualState.Setters>
<Setter Property="TextColor"
Value="Green" />
</VisualState.Setters>
</VisualState>
</VisualStateGroup>
</VisualStateGroupList>
</Setter>
</Style>
<Style TargetType="Image">
<Setter Property="VisualStateManager.VisualStateGroups">
<VisualStateGroupList>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Normal" >
<VisualState.Setters>
<Setter Property="Source"
Value="normalicon.png" />
</VisualState.Setters>
</VisualState>
<VisualState x:Name="Selected">
<VisualState.Setters>
<Setter Property="Source"
Value="normalicon.png" />
</VisualState.Setters>
</VisualState>
<VisualState x:Name="Disabled">
<VisualState.Setters>
<Setter Property="Source"
Value="disableicon.png" />
</VisualState.Setters>
</VisualState>
</VisualStateGroup>
</VisualStateGroupList>
</Setter>
</Style>
</ContentPage.Resources>
I am currently going through the style aspects of a Xamarin Forms Shell Application, I am currently not able to find how to change the FlyoutMenu TextColor.
I have tried adding a style for Label TextColor with the BasedOn being the Base Style I am using, but the text colour still does not change, or it changes all label's TextColor.
<Shell xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="MainShellPage"
xmlns:ios="clr-namespace:Xamarin.Forms.PlatformConfiguration.iOSSpecific;assembly=Xamarin.Forms.Core"
NavigationPage.HasBackButton="False"
FlyoutBackgroundColor="{StaticResource Grey}"
BackgroundColor="{StaticResource Grey}"
IsBusy="{Binding IsBusy}"
BindingContext="{Binding MainShell, Source={StaticResource ViewModelLocator}}"
Navigated="MainShellPage_OnNavigated">
<Shell.Resources>
<Style x:Key="BaseStyle"
TargetType="Element">
<Setter Property="Shell.BackgroundColor"
Value="{StaticResource Grey}" />
<Setter Property="Shell.ForegroundColor"
Value="{StaticResource White}" />
<Setter Property="Shell.TitleColor"
Value="{StaticResource White}" />
<Setter Property="Shell.DisabledColor"
Value="#B4FFFFFF" />
<Setter Property="Shell.UnselectedColor"
Value="#95FFFFFF" />
</Style>
</Shell.Resources>
<Shell.FlyoutHeaderTemplate>
<DataTemplate>
<StackLayout BackgroundColor="{StaticResource Grey}" HeightRequest="180"
Orientation="Vertical">
<Image Source="mobile_logo" HorizontalOptions="Center" VerticalOptions="Start"
WidthRequest="160"/>
<Label Text="{Binding TeamName}" TextColor="{StaticResource White}" FontAttributes="Bold"
HorizontalOptions="Center" />
<Label Text="{Binding UserFullName}" TextColor="{StaticResource White}"
HorizontalOptions="Center" />
<Label Text="{Binding UserEmail}" TextColor="{StaticResource White}"
HorizontalOptions="Center" />
<Label Text="{Binding Version}" TextColor="{StaticResource White}" VerticalOptions="End"
HorizontalOptions="End" Margin="0, 10, 0, 0" FontSize="10" />
</StackLayout>
</DataTemplate>
</Shell.FlyoutHeaderTemplate>
<FlyoutItem FlyoutDisplayOptions="AsMultipleItems">
<Tab Title="TabOne" Style="{StaticResource BaseStyle}"
Route="tabone">
<Tab.Icon>
<FontImageSource
Glyph="{Binding TabOneGlyph, Source={StaticResource MainShellGlyphHelper}}"
FontFamily="{StaticResource FontAwesome}" Color="{StaticResource White}" />
</Tab.Icon>
<ShellContent Title="TabOne" ContentTemplate="{DataTemplate pages:TabOnePage}" />
</Tab>
<Tab Title="TabTwo" Style="{StaticResource BaseStyle}"
Route="tabtwo">
<Tab.Icon>
<FontImageSource
Glyph="{Binding TabTwoGlyph, Source={StaticResource MainShellGlyphHelper}}"
FontFamily="{StaticResource FontAwesome}" Color="{StaticResource White}" />
</Tab.Icon>
<ShellContent ContentTemplate="{DataTemplate pages:TabTwoPage}" />
</Tab>
</FlyoutItem>
<MenuItem />
<MenuItem />
<MenuItem />
<MenuItem
Text="MenuItemOne"
Command="{Binding MenuItemOneCommand}">
<MenuItem.IconImageSource>
<FontImageSource Glyph="{Binding MenuItemOneGlyph, Source={StaticResource MainShellGlyphHelper}}"
FontFamily="{StaticResource FontAwesome}" Color="{StaticResource White}" />
</MenuItem.IconImageSource>
</MenuItem>
I am currently not able to find how to change the FlyoutMenu TextColor.
From this document, Shell includes three style classes, which are automatically applied to FlyoutItem and MenuItem objects. The style class names are FlyoutItemLabelStyle, FlyoutItemImageStyle, and FlyoutItemLayoutStyle.
If you want to change FlyoutMenu Text color, you can create new style and use FlyoutItemLabelStyle to change FlyoutMenu text color.
<Style Class="FlyoutItemLabelStyle" TargetType="Label">
<Setter Property="TextColor" Value="Red" />
</Style>
From Flyout Items document, each ShellContent object can only be accessed through flyout items, and not through tabs. This is because by default, tabs will only be displayed if the flyout item contains more than one tab.
So you need to add Tab in FlyoutItem if you want o display FlyoutMenu.
<FlyoutItem Title="About" Icon="icon_about.png">
<Tab>
<ShellContent ContentTemplate="{DataTemplate local:AboutPage}" Route="AboutPage" />
</Tab>
</FlyoutItem>
<FlyoutItem Title="Browse" Icon="icon_feed.png">
<Tab>
<ShellContent ContentTemplate="{DataTemplate local:ItemsPage}" Route="ItemsPage" />
</Tab>
</FlyoutItem>
There is another way of doing it, you can set an ItemTemplate to your flyout as you did for the header, then you can set a colour for the text:
<Shell.ItemTemplate>
<DataTemplate>
<Grid Grid.Row="0">
<Grid.ColumnDefinitions>
<!-- Icon -->
<ColumnDefinition Width="*"/>
<!-- Title-->
<ColumnDefinition Width="2*"/>
</Grid.ColumnDefinitions>
<!-- Icon -->
<Label Grid.Column="0"
Margin="20,0,0,0"
VerticalOptions="Center"
HorizontalTextAlignment="Center"
HorizontalOptions="Center"
Text="{Binding Icon}"
FontFamily="{StaticResource FontAwesome}"
FontSize="26"
TextColor="{StaticResource White}"/>
<!-- Label -->
<Label Grid.Column="1"
Text="{Binding Title}"
FontSize="23"
VerticalOptions="Center"
VerticalTextAlignment="Center"
TextColor="[ Here you can insert the colour you want]"
HorizontalOptions="Start" />
</Grid>
</DataTemplate>
</Shell.ItemTemplate>
And then, to use it:
<FlyoutItem Title="TabOne"
Icon="{Binding TabOneGlyph, Source={StaticResource
MainShellGlyphHelper}}">
<ShellContent ContentTemplate="{DataTemplate pages:TabOnePage}"/>
</FlyoutItem>
I am doing this technique in most of my project when I sure Shell.
Here's an example of a project I did: https://github.com/bricefriha/AresGaming/blob/master/AresNews/AresNews/AppShell.xaml
I hope this helped you 😉
I wish I could create a BottomBar that would allow me to have a graphic like this, with a button bigger than all the others. I tried with a TabbedPage but it doesn't allow me this customization. I tried with a TabView but it doesn't allow me to insert ContentPage pages by pressing buttons. I would like a BottomBar that would allow me to have this graphic and to be able to use pages as button content.
Example
You could use TabView With Action Button from the Xamarin Community Toolkit linked below. By not assigning any content and instead assigning an event to the TabTapped property, you will display a button instead of another tab.
Install from NuGet: https://www.nuget.org/packages/Xamarin.CommunityToolkit/
Add xct:
xmlns:xct="http://xamarin.com/schemas/2020/toolkit"
Xaml:
<ContentPage.Resources>
<ResourceDictionary>
<ControlTemplate
x:Key="TabItemTemplate">
<Grid
RowSpacing="0">
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Image
Grid.Row="0"
VerticalOptions="Center"
HorizontalOptions="Center"
WidthRequest="24"
HeightRequest="24"
Margin="6"
Source="{TemplateBinding CurrentIcon}" />
<Label
Grid.Row="1"
HorizontalOptions="Center"
FontSize="{TemplateBinding FontSize}"
Text="{TemplateBinding Text}"
TextColor="{TemplateBinding CurrentTextColor}" />
</Grid>
</ControlTemplate>
<ControlTemplate
x:Key="FabTabItemTemplate">
<Grid>
<ImageButton
InputTransparent="True"
Source="circle.png"
Padding="10"
HorizontalOptions="Center"
BackgroundColor="#FF0000"
HeightRequest="60"
WidthRequest="60"
Margin="6">
<ImageButton.CornerRadius>
<OnPlatform x:TypeArguments="x:Int32">
<On Platform="iOS" Value="30"/>
<On Platform="Android" Value="60"/>
<On Platform="UWP" Value="36"/>
</OnPlatform>
</ImageButton.CornerRadius>
<ImageButton.IsVisible>
<OnPlatform x:TypeArguments="x:Boolean">
<On Platform="Android, iOS, UWP">True</On>
<On Platform="macOS">False</On>
</OnPlatform>
</ImageButton.IsVisible>
</ImageButton>
<BoxView
InputTransparent="True"
HorizontalOptions="Center"
CornerRadius="30"
BackgroundColor="#FF0000"
HeightRequest="60"
WidthRequest="60"
Margin="6">
<BoxView.IsVisible>
<OnPlatform x:TypeArguments="x:Boolean">
<On Platform="Android, iOS, UWP">False</On>
<On Platform="macOS">True</On>
</OnPlatform>
</BoxView.IsVisible>
</BoxView>
</Grid>
</ControlTemplate>
<Style
x:Key="TabItemStyle"
TargetType="xct:TabViewItem">
<Setter
Property="FontSize"
Value="12" />
<Setter
Property="TextColor"
Value="#979797" />
<Setter
Property="TextColorSelected"
Value="#FF0000" />
</Style>
<Style
x:Key="CustomTabStyle"
TargetType="xct:TabView">
<Setter
Property="IsTabTransitionEnabled"
Value="True" />
<Setter
Property="TabStripHeight"
Value="48" />
<Setter
Property="TabContentBackgroundColor"
Value="#484848" />
<Setter
Property="TabStripPlacement"
Value="Bottom" />
</Style>
</ResourceDictionary>
</ContentPage.Resources>
<Grid>
<xct:TabView
Style="{StaticResource CustomTabStyle}">
<xct:TabView.TabStripBackgroundView>
<BoxView
Color="#484848"
CornerRadius="36, 36, 0, 0"/>
</xct:TabView.TabStripBackgroundView>
<xct:TabViewItem
Text="Tab 1"
Icon="triangle.png"
ControlTemplate="{StaticResource TabItemTemplate}"
Style="{StaticResource TabItemStyle}">
<Grid
BackgroundColor="LawnGreen">
<Label
HorizontalOptions="Center"
VerticalOptions="Center"
Text="TabContent1" />
</Grid>
</xct:TabViewItem>
<xct:TabViewItem
Text="Tab 2"
Icon="circle.png"
ControlTemplate="{StaticResource FabTabItemTemplate}"
Style="{StaticResource TabItemStyle}"
TabTapped="OnFabTabTapped" />
<xct:TabViewItem
Text="Tab 3"
Icon="square.png"
ControlTemplate="{StaticResource TabItemTemplate}"
Style="{StaticResource TabItemStyle}">
<Grid
BackgroundColor="LightCoral">
<Label
HorizontalOptions="Center"
VerticalOptions="Center"
Text="TabContent3" />
</Grid>
</xct:TabViewItem>
</xct:TabView>
</Grid>
Update:
Do I have the possibility to insert a ContentPage by pressing on in TabViewItem and keep the TabVIew
You could set the page as contentview.
<?xml version="1.0" encoding="utf-8" ?>
<ContentView
x:Class="App9.Page1"
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml">
<ContentView.Content>
<StackLayout>
<Label
HorizontalOptions="CenterAndExpand"
Text="Welcome to Page1!"
VerticalOptions="CenterAndExpand" />
</StackLayout>
</ContentView.Content>
</ContentView>
public partial class Page1 : ContentView
{
public Page1()
{
InitializeComponent();
}
}
And then load in the TabViewItem.
<xct:TabViewItem
ControlTemplate="{StaticResource TabItemTemplate}"
Icon="square.png"
Style="{StaticResource TabItemStyle}"
Text="Tab 3">
<!--<Grid
BackgroundColor="LightCoral">
<Label
HorizontalOptions="Center"
VerticalOptions="Center"
Text="TabContent3" />
</Grid>-->
<local:Page1 />
</xct:TabViewItem>
I'm coding in Visual Studio Mac, using the iPhone 8 simulator running iOS 13.6.
Here's my code for my collection view:
<CollectionView
Margin="0"
VerticalScrollBarVisibility="Never"
BackgroundColor="Transparent"
x:Name="selectableItemsList"
SelectionMode="Multiple"
HorizontalOptions="Center"
VerticalOptions="Center"
SelectionChanged="selectableItemsList_SelectionChanged"
ItemsSource="{Binding Services}">
<CollectionView.ItemTemplate>
<DataTemplate>
<Image
Source="{Binding Image}"
BackgroundColor="Transparent"
HeightRequest="100"
WidthRequest="100"
Aspect="AspectFit"
HorizontalOptions="Center"
VerticalOptions="Start">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup
Name="CommonStates">
<VisualState
Name="Normal" />
<VisualState
Name="Focused" />
<VisualState
Name="Selected">
<VisualState.Setters>
<Setter
Property="BackgroundColor"
Value="Yellow" />
</VisualState.Setters>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
</Image>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
Here's what the items look like when not selected:
And here's what they look like when selected:
See that grey line at the top and left side of each icon? How the heck do I get rid of that?
Is there a way to do a work-around that uses a data trigger to change the image's background color when the item is selected?
Please Note:
Moving the VisualStateManager.VisualStateGroups section into a style in a ResourceDictionary does not solve the problem.
Have a try with put the image inside a Grid:
<CollectionView.ItemTemplate>
<DataTemplate>
<Grid Padding="10">
<Grid.RowDefinitions>
<RowDefinition Height="100" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100" />
</Grid.ColumnDefinitions>
<Image
Source="{Binding .}"
BackgroundColor="Transparent"
HeightRequest="100"
WidthRequest="100"
HorizontalOptions="Center"
VerticalOptions="Start">
</Image>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup
Name="CommonStates">
<VisualState
Name="Normal" />
<VisualState
Name="Focused" />
<VisualState
Name="Selected">
<VisualState.Setters>
<Setter
Property="BackgroundColor"
Value="Yellow" />
</VisualState.Setters>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
</Grid>
</DataTemplate>
</CollectionView.ItemTemplate>