For iOS and WP I override OnElementChange method on ListViewRenderer. But on Android the ListViewRenderer is internal. What is the best method to change the background color for a listview on Android?
Binding background to the model property eg: IsSelected . then you set the value of property。
when you tapped a item set the item's, set the IsSelected property.
//============eg============
public class SceneCellModel :BaseModel
{
public SceneCellModel()
{
}
public string SceneName { get;set; }
bool isSelected =false;
public bool IsSelected {
get { return isSelected; }
set { SetProperty(ref isSelected, value, "IsSelected"); }
}
}
//============= DataTemplate =========
...
var grid = new Grid()
{
Padding = new Thickness(0, 5),
ColumnSpacing = 0,
grid.SetBinding(Grid.BackgroundColorProperty, new Binding("IsSelected",converter: new IsSelectedToBackgroundConvert(), converterParameter: grid.BackgroundColor ));
I missed that it was so simple on android, on iOS it doesn't work, but now I know that it is because I had to set the color on every cell in the ListView because they have a default color.
// Build the template for the menu item
var cell = new DataTemplate(typeof(ImageCell));
cell.SetBinding(TextCell.TextProperty, "Title");
cell.SetBinding(ImageCell.ImageSourceProperty, "IconSource");
cell.SetValue (VisualElement.BackgroundColorProperty, Color.Transparent);
listview.ItemTemplate = cell;`
The listview itself is transparent (my page background color is Red), but the populated cells are white. Am I doing something wrong?
@ClaudiuTomescu yeah, it looks like the default white color that is being set on the UITableView cell on iOS can't be set to Transparent, so for now you can only set the color - including set the color of each cell background (to have it work with Xamarin.Forms on iOS).
@JamesPettigrew
I don't believe ViewCell has a bindable property for background color. What I do to get around this is set the background color of the layout that contains everything in the cell (in the custom cell class, in your case ThermostatCell) and make sure it's layout options are set to fill the whole cell.
Posts
is this what you're looking for?
What if you wanted to change the selected item background color? Don't tell me to write a renderer either. LOL
Well, that does seem to be the defacto answer around here ... lol
Binding background to the model property eg: IsSelected . then you set the value of property。
when you tapped a item set the item's, set the IsSelected property.
//============eg============
public class SceneCellModel :BaseModel
{
public SceneCellModel()
{
}
//============= DataTemplate =========
...
var grid = new Grid()
{
Padding = new Thickness(0, 5),
ColumnSpacing = 0,
grid.SetBinding(Grid.BackgroundColorProperty, new Binding("IsSelected",converter: new IsSelectedToBackgroundConvert(), converterParameter: grid.BackgroundColor ));
I missed that it was so simple on android, on iOS it doesn't work, but now I know that it is because I had to set the color on every cell in the ListView because they have a default color.
Does anyone know how can I make a ListView's background transparent? Here is a snippet of the code I'm working with:
`var listview = new ListView {
ItemsSource = items,
VerticalOptions = LayoutOptions.FillAndExpand,
BackgroundColor = Color.Transparent,
};
listview.ItemTemplate = cell;`
The listview itself is transparent (my page background color is Red), but the populated cells are white. Am I doing something wrong?
Thanks!
@ClaudiuTomescu yeah, it looks like the default white color that is being set on the UITableView cell on iOS can't be set to
Transparent
, so for now you can only set the color - including set the color of each cell background (to have it work with Xamarin.Forms on iOS).I using a custom renderer for setting each cell to transparent.
Does anybody know why I can't set the cell colour? ThermostatCell inherits from ViewCell.
var cell = new DataTemplate(typeof (ThermostatCell)); cell.SetValue(VisualElement.BackgroundColorProperty, Color.Black);
@JamesPettigrew
I don't believe ViewCell has a bindable property for background color. What I do to get around this is set the background color of the layout that contains everything in the cell (in the custom cell class, in your case ThermostatCell) and make sure it's layout options are set to fill the whole cell.
I have written a blog post about how I did, http://danielhindrikes.se/xamarin/xamarin-forms-change-background-color-on-ios-listview/
@dhindrik
That's super simple and works great!