Changing default color of ListView in XAML

Raymond Gilbers 176 Reputation points
2022-09-05T18:40:02.66+00:00

Hello All,

I'm trying to get a grasp of XAML but all default color of each attribute I place in the form is white. So this is quite annoying to see what I'm doing.

Now I'm busy following some tutors and I try to change the Foreground of the `ListView' but I need to do set up the color of each item separately I think there must be a easier way.

            <ListView Grid.Row="1">  
                <ListView.ItemTemplate>  
                    <DataTemplate>  
                        <Grid>  
                            <Border Background="Blue" Height="200" Width="200">  
                                <TextBlock Text="{Binding}"   
                               FontSize="10" Foreground="Black"/>  
                            </Border>  
                        </Grid>  
                    </DataTemplate>  
  
                </ListView.ItemTemplate>  
                    <ListViewItem Foreground="#FF0E0D0D">Thomas</ListViewItem>  
                    <ListViewItem>Julia</ListViewItem>  
                    <ListViewItem>Anna</ListViewItem>  
                    <ListViewItem>Anna</ListViewItem>  
                    <ListViewItem>Anna</ListViewItem>  
                    <ListViewItem>Anna</ListViewItem>  
                    <ListViewItem>Anna</ListViewItem>  
                    <ListViewItem>Anna</ListViewItem>  
                    <ListViewItem>Anna</ListViewItem>  
                    <ListViewItem>Alicia</ListViewItem>                  
            </ListView>  

 

I copied the example which I found on the MSDN forum into my code and would have expected to see some changes but I don't see anything changing. Does anybody see why this is the case?

.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,367 questions
Universal Windows Platform (UWP)
0 comments No comments
{count} votes

Accepted answer
  1. Roy Li - MSFT 31,766 Reputation points Microsoft Vendor
    2022-09-06T02:42:39.91+00:00

    Hello,

    Welcome to Microsoft Q&A!

    Update:

    If you are add the ListViewItem manually in the Xaml and you want to set the color for all the ListViewItem, you just need to change the ItemContainerStyle of the ListView. Set a new value to the Foreground property.

       <ListView x:Name="MyListView">  
              
        <!--  <ListView.ItemContainerStyle>  
                <Style TargetType="ListViewItem">  
                    <Setter Property="Foreground" Value="Red" />  
                </Style>  
            </ListView.ItemContainerStyle>  -->  
                <ListViewItem>Julia</ListViewItem>  
                <ListViewItem>Anna</ListViewItem>  
                <ListViewItem>Anna</ListViewItem>  
                <ListViewItem>Anna</ListViewItem>  
            </ListView>  
      
          
      
    

    ------------------------------------------------------------------------------------

    I'm trying to get a grasp of XAML but all default color of each attribute I place in the form is white.

    I noticed that you are setting the ItemDataplate for the ListView and setting the ListViewItem manually at the same time. This will lead to a conflict. In the end, the ListView shows the ListViewItem that you explicitly set. This is the reason for the behavior that you are getting.

    You could remove the ListViewItem objects and give the ListView a source to show the item which uses the DataTemplate that you defined.

    I try to change the Foreground of the `ListView' but I need to do set up the color of each item separately I think there must be a easier way

    If you set the ListViewItem manually, then you will need to add the Foreground for each of them manually as well.

    I'd suggest that you could use Data-binding to show the item content and define different colors for the TextBlock.

    Thank you.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our [documentation][2] to enable e-mail notifications if you want to receive the related email notification for this thread. [2]: https://learn.microsoft.com/en-us/answers/articles/67444/email-notifications.html


1 additional answer

Sort by: Most helpful
  1. Castorix31 81,636 Reputation points
    2022-09-05T20:01:48.03+00:00

    See samples in MSDN docs , like at : item-containers-templates

    0 comments No comments