Share via

How to set dynamically the stacklayout backgroundcolour in the List view (.net Maui)?

Sowndarrajan Vijayaragavan 470 Reputation points
2023-05-05T09:50:07.47+00:00
1.In XAML
<ListView x:Name="lvWires" ItemsSource="{Binding Wires}" >
	<ListView.ItemTemplate  >
		<DataTemplate >
			<ViewCell>
				<StackLayout BackgroundColor="{Binding BgColor}">
					<Label Text="Test" />
				</StackLayout>
			</ViewCell>
		</DataTemplate>
	</ListView.ItemTemplate>
</ListView>
///////////////////////////////////////////////////////////////////
2.In VM
[ObservableProperty]
public ObservableCollection<Wire> wires;

Wires.Add(new Wire { BgColor = "Red" });

i had hard cord it to red but it is not updating in UI
                
Developer technologies | .NET | .NET Multi-platform App UI

2 answers

Sort by: Most helpful
  1. Sowndarrajan Vijayaragavan 470 Reputation points
    2023-05-08T12:03:04.6033333+00:00
    public partial class Wire : ObservableObject
    {
    		[ObservableProperty]
            Color bgColor{ get; set; }
           .....
    }
    

    Was this answer helpful?

    0 comments No comments

  2. Alessandro Caliaro 4,206 Reputation points
    2023-05-05T10:27:27.98+00:00

    You have to implement INotifyPropertyChanged in your Wire class.

    I think you can do something like

    public partial class Wire : ObservableObject
    {
    		[ObservableProperty]
            string bgColor{ get; set; }
           .....
    }
    

    Was this answer helpful?


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.