Difficulties to change UWP control colors

Raymond Gilbers 176 Reputation points
2022-09-06T19:13:53.06+00:00

Hello

I'm new in XAML but I have some C# experience. I thought lets dive into XAML as it looks nice and (I thought is worth the afford). But I start to think that this is way to complicated.

In WPF I could select a textbox change labels easy with something like 'label1.forecolor'.

Yesterday I just wanted to change the foreground color of a ListViewItem and this is 5 lines of code.

Now I want to change a Textbox header forecolor and I can search what I want on Google or MSDN but I do not find any example that makes sense to me or works. (probably my lack of understanding). But one thing is for sure I do not see the logic of how these properties work. I would assume that it should be pretty straight forward.

First the code that was suggested (and is working perfect) for a ListViewItem

<ListView Grid.Row="1" Background="LightGray">  
   <ListView.ItemContainerStyle>  
        <Style TargetType="ListViewItem">  
              <Setter Property="Foreground" Value="Black" />  
          </Style>  
   </ListView.ItemContainerStyle>  
   <ListViewItem>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>  

Next I found on MSDN that (if I understand it correctly) I should use the property `TextControlHeaderForeground'

One of the things I have tried is as below (and is not working)

       <StackPanel Grid.Row="1" Grid.Column="1" Background="LightGray" >  
            <TextBox   
                x:Name="TextBox1"  
                Header="Firstname"   
                Margin="10"  
                TextControlHeaderForeground ="Blue"  
                />  
            <TextBox Header="Lastname" Margin="10"/>  
            <CheckBox x:Name="isDeveloper" Content="Is developer" Margin="10" Foreground="#FF131010">  
                <CheckBox.Background>  
                    lightgray  
                </CheckBox.Background>                      
            </CheckBox>  
        </StackPanel>  

And I haven't even started to try to change the Foreground of the Checkbox LOL. I trully beleive that XAML have some logic behind all this but I can't see it at the moment. Does someone want to help me out a bit?

Thanks a lot

Universal Windows Platform (UWP)
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,218 questions
{count} votes

Accepted answer
  1. Roy Li - MSFT 31,681 Reputation points Microsoft Vendor
    2022-09-07T03:06:54.83+00:00

    Hello,

    Welcome to Microsoft Q&A!

    I want to change a Textbox header forecolor

    The header of the TextBox is using a theme resource called TextControlHeaderForeground as its foreground color value. You could just override this value in the TextBox.Resources

    Like this:

    <TextBox Header="This is the TextBox" Width="200" Height="80">  
                <TextBox.Resources>  
                    <SolidColorBrush x:Key="TextControlHeaderForeground" Color="Red" />  
                </TextBox.Resources>            
     </TextBox>  
    

    The result looks like this:
    238424-screenshot-2022-09-07-110743.png

    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 to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

0 additional answers

Sort by: Most helpful