Exception in ListView when defining ItemStyle and SelectedItemStyle BackgroundColor in C# instead of XAML

1 Answer 50 Views
ListView
Brad
Top achievements
Rank 2
Brad asked on 15 Jul 2023, 12:56 PM | edited on 15 Jul 2023, 01:02 PM

If I set ListView Background colors in XAML, they work, but if I set them in code, they don't.

This works:

<telerik:RadListView x:Name="MyList">
    <telerik:RadListView.ItemStyle>
        <telerik:ListViewItemStyle BackgroundColor="Transparent"/>
    </telerik:RadListView.ItemStyle>
    <telerik:RadListView.SelectedItemStyle>
        <telerik:ListViewItemStyle BackgroundColor="DarkRed"/>
    </telerik:RadListView.SelectedItemStyle>
    <telerik:RadListView.ItemTemplate>
        <DataTemplate>
            <telerik:ListViewTemplateCell>
                <telerik:ListViewTemplateCell.View>
                    ...
                </telerik:ListViewTemplateCell.View>
            </telerik:ListViewTemplateCell>
        </DataTemplate>
    </telerik:RadListView.ItemTemplate>
</telerik:RadListView>

This does not:

<telerik:RadListView x:Name="MyList">
    <telerik:RadListView.ItemTemplate>
        <DataTemplate>
            <telerik:ListViewTemplateCell>
                <telerik:ListViewTemplateCell.View>
                    ...
                </telerik:ListViewTemplateCell.View>
            </telerik:ListViewTemplateCell>
        </DataTemplate>
    </telerik:RadListView.ItemTemplate>
</telerik:RadListView>
// Constructor
public MyListView()
{
    InitializeComponent();

    MyList.ItemStyle.BackgroundColor = Colors.Transparent;
    MyList.SelectedItemStyle.BackgroundColor = Colors.DarkRed;
}

Defining the BackgroundColor in code in this way generates an exception. What am I doing wrong?

(Using: Telerik.UI.for.Maui 5.1.0)

1 Answer, 1 is accepted

Sort by
0
Accepted
Lance | Manager Technical Support
Telerik team
answered on 17 Jul 2023, 02:30 PM | edited on 17 Jul 2023, 09:13 PM

Hi Brad,

The reason for the exception is because you're assigning a color value to a null object. Let me take a step back to better explain.

When you define an object in XAML it becomes an instance of that object. In this situation, notice how you're not directly setting a property of RadListView with the color value... instead you're creating an instance of ListViewItemStyle setting the color of that, and finally assigning that new instance to the RadListView's ItemStyle/SelectedItemStyle properties.

So, if you're going to do that in code-behind, you must also create the instances of ListViewItemStyle

public MyListView()
{
    InitializeComponent();

    MyList.ItemStyle = new ListViewItemStyle { BackgroundColor = Colors.Transparent };
    MyList.SelectedItemStyle = new ListViewItemStyle { BackgroundColor = Colors.DarkRed };
}

Let me know if you have any further trouble.

Regards,
Lance | Manager Technical Support
Progress Telerik

[edit] Fixed misspellings.

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Brad
Top achievements
Rank 2
commented on 17 Jul 2023, 11:25 PM

Hi Lance, thanks for the explanation. I had assumed there would be default objects already applied.

Cheers,

Brad

Lance | Manager Technical Support
Telerik team
commented on 18 Jul 2023, 11:41 AM

I'm happy to hear you're up and running again. Yeah, I can understand why you'd go directly to set the property, this is just one of those uncommon situations where there is no default value because you're overriding the default styles (rather than editing the default styles).
Tags
ListView
Asked by
Brad
Top achievements
Rank 2
Answers by
Lance | Manager Technical Support
Telerik team
Share this question
or