如何更改特定元素(而不是全局元素)的 TextBox 占位符文本颜色

2023-11-23

MSDN 列出了TextBox class here。我可以通过创建一个来覆盖这些主题资源ResourceDictionary in App.xaml像这样:

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

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

我试过把这本字典放进去Page.Resources乃至TextBox.Resources为了TextBox我想应用它,但它不起作用。

I really不想重新定义Template只是为了改变这个属性。


EDITHeena 的答案很接近,但我还想为浅色和深色主题设置不同的颜色,因为我的文本框有透明的背景颜色。

我通过保持Foreground="{ThemeResource TextBoxPlaceholderTextThemeBrush}"作为Template(因此,换句话说,该模板正是 MSDN 中的默认模板),然后在页面资源中指定:

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

but现在这意味着我必须投入大量资金ControlTemplate我的页面资源中的文本框的样式设置器也一样,无论如何,它只是默认值的精确复制!

这与如何做有关系吗TextBoxPlaceholderTextThemeBrush是从内部解决的ControlTemplate?即它发现我的自定义主题字典的原因是因为ControlTemplate是在同一个资源字典中定义的吗?

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


假设您正在使用 MSDN 文本框Style

Resource从模板中的内容控制中删除前景属性<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>

enter image description here

Update

Resource

从模板中的内容控制中删除前景属性<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>

enter image description here

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何更改特定元素(而不是全局元素)的 TextBox 占位符文本颜色 的相关文章

随机推荐

  • 为应用程序选择多个 Internet 连接之一

    我有一台具有几种不同互联网连接的计算机 LAN WLAN WiFi 或 3G 所有这些都是活动的 机器可以使用其中任何一个 现在我想告诉我的应用程序使用可用的连接之一 例如 我想告诉我的应用程序仅使用 WiFi 而其他软件可能使用其他功能
  • Android - 自定义语音操作的意图

    当我使用谷歌语音搜索时 我可以说 发送文本 谷歌将启动我的短信程序 我可以说 听 谷歌将启动我的默认音乐应用程序 我的应用程序是否可以注册它自己的 特殊短语 例如 MyApp DoSomething 然后让 Google 启动 MyApp
  • 更优雅的多行 JavaScript 字符串方法

    我知道如何在不使用 的情况下打印大字符串的唯一方法是使用 反斜杠 丑陋的 div div 有没有办法在 longString 不受污染的情况下做到这一点 php 有 foo 长多行字符串
  • Android Actionbar 设置选项卡宽度

    目前我正忙于为我的公司开发 WHMCS 应用程序 作为导航 我想使用操作栏中的选项卡功能 但是 如何编辑选项卡的宽度 我需要在一个屏幕上同时显示 5 个内容 而无需滚动 我已经尝试过一些这样的样式
  • 如何在样式表中的 URL 中换行

    我有一个包含很长行 数据网址 的样式表 无论如何 我可以将这些行分成更小的行吗 长线示例 background image url data image png base64 really long string 您可以通过将 URI 用引
  • 圆与圆段碰撞

    我正在努力寻找一个可靠的解决方案来检测圆和圆段之间的碰撞 想象一下游戏敌人的视场锥体 其中圆圈代表感兴趣的物体 底部的图表是我为了尝试解决一些可能的情况而绘制的 但我确信还有更多 我了解如何快速排除极端情况 我丢弃任何不与整个圆碰撞的目标
  • 如果 WebAppContext 启动失败,如何取消启动或关闭 jetty

    我正在使用 Maven Jetty 插件 如果任何 bean 失败 有没有办法阻止服务器启动或关闭 例如使用 maven jetty plugin 这是一个例子 2013 04 22 11 43 59 327 WARN oejw WebAp
  • Python中通过套接字发送和接收对象

    我在互联网上搜索了很多 但我还没有找到通过套接字发送对象并按原样接收它的解决方案 我知道它需要酸洗 我已经完成了 另一方面 它将其转换为字节并被接收 但是如何将这些字节转换为该类型的对象呢 process time data current
  • Homebrew 升级后 Jupyter Notebook 模块出现错误

    我已经使用 Jupyter 一段时间了 它运行得很好 我通过 Homebrew 安装了 Jupyter 和 Python 我在 MacOS 上运行 昨天 我运行了命令brew upgrade现在我的 Jupyter 笔记本无法找到任何已安装
  • JavaFX - SplitPane、调整大小和比例

    如何实现整个 SplitPane 的按比例调整大小 public class JfxSplitPaneTest extends Application Override public void start Stage stage throw
  • Ubuntu 14.04 - pm2 启动后重新启动后未启动

    我正在使用 pm2 启动 node js 进程 我希望在系统 Intel Atom 处理器上的 Ubuntu 14 04 启动时自动启动该进程 我已按照以下说明进行操作pm2 网站但没有成功 我的 dump pm2 似乎是正确的 但 pm2
  • 如何获取.Net Core中进程的CPU使用率和虚拟内存?

    在 NET Core中 如何获取给定进程的CPU使用率和虚拟内存 Google 搜索结果显示 PerformanceCounter 和 DriverInfo 类可以完成这项工作 但是 PerformanceCounter 和 DriverI
  • 宏不允许定义词法变量

    此代码使用 实验性 宏 use experimental macros macro new var quasi my a 42 new var say a 失败与Variable a is not declared 尽管宏通过时没有错误 如
  • 将数据库结果添加到数组

    我现在脑子一片空白 一辈子也想不出解决办法 我的场景是使用 PHP 和 MySQL 进行编程 我有一个数据库表返回特定 orderid 的结果 该查询可以为每个订单最多返回 4 行 最少返回 1 行 这是我想要如何返回结果的图像 替代文本
  • 我可以对 JScript (.js) 或 VBScript (.vbs) 文件进行数字签名吗?

    我知道可以签署 Windowsbinary可执行文件使用signtool 所以一直以来我都假设人们无法签署由Windows Script Host 例如 JScript js 或 VBScript vbs 因为它们只是文本文件 但今天 在打
  • 使用 preg_match 查找所有 PHP 变量

    如何找到所有 PHP 变量preg match 我制作了以下正则表达式 string Hallo var blabla var iam a var varvarvar gfg djf jdfgjh fd variable instring
  • 使用 Swift 制作可点击的 UILabel

    我想将特定单词设置为可点击UILabel使用 Swift 编写文本 是否可以 如果这里有多个标签 我如何检测按下的是哪个单词 你不能用简单的标签来做 github上有可用的库 https github com TTTAttributedLa
  • 如何检查时间偏移是否处于夏令时?

    我使用的 CMS 允许我访问用户相对于 UTC GMT 时间的时间偏移 因此 如果用户将时区设置为 EST 则此偏移量将为 5 当我需要显示当前时间时 我现在正在做的事情是这样的 date M j Y h i A time offset 3
  • 如何确定 iOS 上的设置何时更改

    我使用标准创建了一个自定义 Settings app 捆绑包root plistiPhone 的方法 我想知道是否有一种方法可以确定用户何时更改我的应用程序中的这些设置 您可以使用以下命令监听 NSUSerDefaults DidChang
  • 如何更改特定元素(而不是全局元素)的 TextBox 占位符文本颜色

    MSDN 列出了TextBox class here 我可以通过创建一个来覆盖这些主题资源ResourceDictionary in App xaml像这样