I want to change the Background color of my ListViewItems based on the value of the boolean property EstaGuardado of my model class Permiso.
namespace Modelo.Autenticacion
{
public class Permiso
{
public int Id { get; set; }
public string Nombre { get; set; }
public bool EstaGuardado { get; set; } = false;
}
}
The permissions are loaded on page initialization alongside the roles.
When a Rol is selected, the permissions are loaded into the ObservableCollection<Permiso> FilteredPermisos which is used by the PermisosListView:
private void OnRolSeleccionado(object sender, SelectionChangedEventArgs e)
{
_rolSeleccionado = RolesListView.SelectedItem as Rol;
if (_rolSeleccionado != null)
{
Permisos.Clear();
FilteredPermisos.Clear();
foreach (var permiso in _rolSeleccionado.Permisos)
{
Permisos.Add(permiso);
FilteredPermisos.Add(permiso);
}
// Update the header text with the selected role name
PermisosHeaderTextBlock.Text = $"Permisos del rol {_rolSeleccionado.Nombre}";
}
}
The XAML:
<Page.Resources>
<local2:EstaGuardadoToBackgroundConverter x:Key="EstaGuardadoToBackgroundConverter"/>
</Page.Resources>
<ListView x:Name="PermisosListView" Grid.Row="1" ItemsSource="{x:Bind FilteredPermisos, Mode=OneWay}">
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<ItemsWrapGrid Orientation="Horizontal" MaximumRowsOrColumns="5"/>
</ItemsPanelTemplate>
</ListView.ItemsPanel>
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem" BasedOn="{StaticResource ListViewItemRevealStyle}">
<Setter Property="Margin" Value="2"/>
<Setter Property="HorizontalContentAlignment" Value="Center"/>
<Setter Property="VerticalContentAlignment" Value="Center"/>
<Setter Property="Background" Value="{Binding EstaGuardado, Converter={StaticResource EstaGuardadoToBackgroundConverter}, Mode=OneWay}"/>
<Setter Property="MinWidth" Value="150"/>
<Setter Property="Padding" Value="0"/>
</Style>
</ListView.ItemContainerStyle>
<ListView.ItemTemplate>
<DataTemplate x:DataType="local1:Permiso">
<StackPanel Orientation="Vertical" Padding="5" Spacing="5" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<StackPanel Orientation="Horizontal" HorizontalAlignment="Stretch" VerticalAlignment="Top">
<TextBlock Text="{Binding Nombre}" FontWeight="Bold" FontSize="14" Grid.Column="0" VerticalAlignment="Center"/>
<Button Content="X" Click="OnEliminarPermisoClick" Tag="{Binding}" Grid.Column="1" Margin="5,0,0,0"/>
</StackPanel>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Top" Spacing="5">
<TextBlock Text="ID:" Style="{StaticResource FooterTextStyle}"/>
<TextBlock Text="{Binding Id}" Style="{StaticResource FooterTextStyle}"/>
</StackPanel>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</Grid>
However, the EstaGuardadoToBackgroundConverter is never called.
I managed to get the Converter called by using it inside the <ListView.ItemTemplate> block instead. However, the background is being set on the StackPanel of my template, not on the base container of the ListViewItem, which leaves gaps on the sides even after setting the padding to zero.
<ListView.ItemTemplate>
<DataTemplate x:DataType="local1:Permiso">
<StackPanel Orientation="Vertical" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Background="{Binding EstaGuardado, Converter={StaticResource EstaGuardadoToBackgroundConverter}, Mode=OneWay}">


