简体   繁体   中英

Change the textbox header color in C# UWP

I want to change the color of the Textbox header in UWP. My Textbox:

<TextBox x:Name="tbFullName" Header="Full Name" Margin="30,24,0,0" MaxLength="320" Width="400" HorizontalAlignment="Left" InputScope="PersonalFullName" VerticalAlignment="Stretch"/>

My current (not working) code to change the color:

tbFullName.Header = new SolidColorBrush(Windows.UI.Colors.White);

I hope someone is able to help. Note: I'm am very new to UWP and I'm quite new to programming, I would really appreciate if the answers given to me aren't all too hard to understand. Thanks in advance!

For your requirement, you could custom TextBox's HeaderTemplate like the follow.

<TextBox HorizontalAlignment="Stretch"  VerticalAlignment="Stretch" FontSize="12" Header="Name" >
    <TextBox.HeaderTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding}" Foreground="Red" />
        </DataTemplate>
</TextBox.HeaderTemplate>

And modify the Foreground the for TextBlock that contained in DataTemplate.

Update The foreground theme resource of HeaderContentPresenter is TextControlHeaderForeground you could also override it in app resource like the follow

 <Application.Resources>
    <SolidColorBrush x:Key="TextControlHeaderForeground" Color="Red" />
</Application.Resources>

Usage

<TextBox Text="Cotent" Height="100" Header="Name"/>

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM