如何更改特定元素而非全局元素的 TextBox 占位符文本颜色 [英] How to change TextBox placeholder text color for a specific element, not global

查看:26
本文介绍了如何更改特定元素而非全局元素的 TextBox 占位符文本颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

MSDN 列出了 TextBox 类的样式和模板这里.我可以通过在 App.xaml 中创建一个 ResourceDictionary 来覆盖这些主题资源,如下所示:

MSDN lists the styles and templates for the TextBox class here. I can override these theme resources by creating a ResourceDictionary in App.xaml like this:

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.ThemeDictionaries>
            <ResourceDictionary x:Key="Default">
                <SolidColorBrush x:Key="TextBoxPlaceholderTextThemeBrush" Color="Yellow"/>
            </ResourceDictionary>
        </ResourceDictionary.ThemeDictionaries>
    </ResourceDictionary>
</Application.Resources>

但这会影响我的应用程序中的每个 TextBox.如何仅为特定元素设置此主题?

but this will affect every TextBox in my app. How can I set this theme for only a specific element?

我已经尝试将这本字典放在 Page.Resources 中,甚至将 TextBox.Resources 放在我想要应用它的 TextBox 中,但它不起作用.

I've tried putting this dictionary in Page.Resources and even TextBox.Resources for the TextBox that I want to apply it to, but it doesn't work.

真的不想为了改变这个属性而重新定义Template.

I really don't want to have to redefine the Template just to change this property.

编辑 Heena 的回答很接近,但我还想为浅色和深色主题设置不同的颜色,因为我的文本框具有透明的背景色.

EDIT Heena's answer is close, but I would also like to set different colors for light and dark themes because my textbox has a transparent background color.

我设法通过将 Foreground="{ThemeResource TextBoxPlaceholderTextThemeBrush}" 作为 Template 的一部分来实现这一点(因此,换句话说,模板正是默认的根据 MSDN),然后在页面资源中指定:

I managed to achieve this by keeping Foreground="{ThemeResource TextBoxPlaceholderTextThemeBrush}" as part of the Template (so, in other words, the template is exactly the default as per MSDN) and then specifying in the page resources:

<Page.Resources>
    <ResourceDictionary.ThemeDictionaries>
        <ResourceDictionary x:Key="Light">
            <SolidColorBrush x:Key="TextBoxPlaceholderTextThemeBrush" Color="Blue"/>
        </ResourceDictionary>
        ...
    </ResourceDictionary.ThemeDictionaries>
</Page.Resources>

但是这现在意味着我也必须在我的页面资源中为文本框放置一个巨大的 ControlTemplate 样式设置器,无论如何它只是默认值的完全重复!

but this now means I have to put a huge ControlTemplate style-setter for the textbox in my page resources too, which is just an exact duplicate of the default anyway!

这是否与 TextBoxPlaceholderTextThemeBrushControlTemplate 中的解析方式有关?即它发现我的自定义主题字典的原因是因为 ControlTemplate 是在同一个资源字典中定义的?

Does this have something to do with how TextBoxPlaceholderTextThemeBrush is resolved from within the ControlTemplate? i.e. the reason why it discovers my custom theme dictionary is because the ControlTemplate was defined in the same resource dictionary?

这应该怎么做?我是否应该只对文本框进行子类化,以便所有 XAML 都可以移动到另一个文件(即使它只是用于一个文本框)?

How is this supposed to be done? Should I just subclass the textbox so that all that XAML can be moved to another file (even if it is just for one textbox)?

推荐答案

假设您使用的是 MSDN Textbox 样式

Assuming you are using MSDN Textbox Style

资源从 Template<ContentControl Foreground="{ThemeResource TextBoxPlaceholderTextThemeBrush}"/>

<Page.Resources>
    <!--From MSDN : Default style for Windows.UI.Xaml.Controls.TextBox -->
    <Style x:Key="MsdnTextboxStyle" TargetType="TextBox">          
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="TextBox">
                 .....
                 .....
                <ContentControl x:Name="PlaceholderTextContentPresenter"
                              Grid.Row="1"                          
                              Margin="{TemplateBinding BorderThickness}"
                              Padding="{TemplateBinding Padding}"
                              IsTabStop="False"
                              Grid.ColumnSpan="2"
                              Content="{TemplateBinding PlaceholderText}" 
                              IsHitTestVisible="False"/>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>           
</Page.Resources> 

Xaml

 <StackPanel Orientation="Horizontal">
    <TextBox  PlaceholderText="PlaceholderText here..." Style="{StaticResource MsdnTextboxStyle}" Margin="20"  Foreground="Red" Height="30" Width="120">
        <TextBox.Resources>
            <Style TargetType="ContentControl">
                <Setter Property="Foreground" Value="Green"/>
            </Style>
        </TextBox.Resources>
    </TextBox>
    <TextBox  PlaceholderText="PlaceholderText here..." Style="{StaticResource MsdnTextboxStyle}" Margin="20"  Foreground="Red" Height="30" Width="120">
        <TextBox.Resources>
            <Style TargetType="ContentControl">
                <Setter Property="Foreground" Value="Red"/>
            </Style>
        </TextBox.Resources>
    </TextBox>
    <TextBox  PlaceholderText="PlaceholderText here..." Style="{StaticResource MsdnTextboxStyle}" Margin="20"  Foreground="Red" Height="30" Width="120">
        <TextBox.Resources>
            <Style TargetType="ContentControl">
                <Setter Property="Foreground" Value="Blue"/>
            </Style>
        </TextBox.Resources>
    </TextBox>
</StackPanel>

更新

资源

从模板中的 Contencontrol 中删除前景属性<ContentControl Foreground="{ThemeResource TextBoxPlaceholderTextThemeBrush}"/>

Remove Foreground Property from Contencontrol in Template<ContentControl Foreground="{ThemeResource TextBoxPlaceholderTextThemeBrush}"/>

<Page.Resources>
    <ResourceDictionary>
        <ResourceDictionary.ThemeDictionaries>
            <ResourceDictionary x:Key="Default">
                <SolidColorBrush x:Key="ContentControlForeGround" Color="Red"></SolidColorBrush>
                <SolidColorBrush x:Key="ContentControlForeGround1" Color="Yellow"></SolidColorBrush>
            </ResourceDictionary>
            <ResourceDictionary x:Key="Light">
                <SolidColorBrush x:Key="ContentControlForeGround" Color="Blue"></SolidColorBrush>
                <SolidColorBrush x:Key="ContentControlForeGround1" Color="SkyBlue"></SolidColorBrush>
            </ResourceDictionary>
            <ResourceDictionary x:Key="Dark">
                <SolidColorBrush x:Key="ContentControlForeGround" Color="Green"></SolidColorBrush>
                <SolidColorBrush x:Key="ContentControlForeGround1" Color="Chocolate"></SolidColorBrush>
            </ResourceDictionary>
        </ResourceDictionary.ThemeDictionaries>
        <Style x:Key="TextBoxStyle1" TargetType="TextBox">
          .....
           <ContentControl x:Name="PlaceholderTextContentPresenter" Grid.ColumnSpan="2" Content="{TemplateBinding PlaceholderText}"  IsHitTestVisible="False" IsTabStop="False" Margin="{TemplateBinding BorderThickness}" Padding="{TemplateBinding Padding}" Grid.Row="1"/>
           ......                                                       
        </Style>
    </ResourceDictionary>
</Page.Resources>

xaml

<StackPanel Orientation="Horizontal">
    <TextBox  Style="{StaticResource TextBoxStyle1}" PlaceholderText="PlaceholderText here..."  Margin="20"  Foreground="Red" Height="30" Width="170">
        <TextBox.Resources>
            <Style TargetType="ContentControl">
                <Setter Property="Foreground" Value="{StaticResource ContentControlForeGround}"></Setter>
            </Style>
        </TextBox.Resources>
    </TextBox>
    <TextBox  Style="{StaticResource TextBoxStyle1}" PlaceholderText="PlaceholderText here..."  Margin="20"  Foreground="Red" Height="30" Width="170">
        <TextBox.Resources>
            <Style TargetType="ContentControl">
                <Setter Property="Foreground" Value="{StaticResource ContentControlForeGround1}"></Setter>
            </Style>
        </TextBox.Resources>
    </TextBox>
</StackPanel>

这篇关于如何更改特定元素而非全局元素的 TextBox 占位符文本颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆