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

标签 xaml windows-runtime windows-store-apps windows-8.1

MSDN 列出了 TextBox 的样式和模板。类(class) here .我可以通过创建 ResourceDictionary 来覆盖这些主题资源在 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我想应用它,但它不起作用。

我真的不想重新定义 Template只是为了改变这个属性。

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

我设法通过保留 Foreground="{ThemeResource TextBoxPlaceholderTextThemeBrush}" 来实现这一点作为 Template 的一部分(因此,换句话说,该模板正是 MSDN 中的默认模板),然后在页面资源中指定:
<Page.Resources>
    <ResourceDictionary.ThemeDictionaries>
        <ResourceDictionary x:Key="Light">
            <SolidColorBrush x:Key="TextBoxPlaceholderTextThemeBrush" Color="Blue"/>
        </ResourceDictionary>
        ...
    </ResourceDictionary.ThemeDictionaries>
</Page.Resources>

但这现在意味着我必须放一个巨大的 ControlTemplate我的页面资源中文本框的样式 setter ,无论如何它只是默认的完全相同!

这和TextBoxPlaceholderTextThemeBrush的方式有关系吗?从 ControlTemplate 中解决?即它发现我的自定义主题字典的原因是因为 ControlTemplate在同一个资源字典中定义?

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

最佳答案

假设您使用的是 MSDN 文本框 Style

资源
从模板中的 Contencontrol 中删除前景属性 <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

更新

资源

从模板中的 Contencontrol 中删除前景属性 <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>

xml
<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

关于xaml - 如何更改特定元素的 TextBox 占位符文本颜色,而不是全局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24429522/

相关文章:

c# - 如何在代码隐藏中创建 ResourceDictionary?

c# - .Xaml 不呈现代码中写入的 unicode?

WPF 可执行文件不会在 Visual Studio 之外运行(资源字典问题)

c# - 当缩放中心因捏合(缩放)或 metro 风格应用程序中的其他手势而发生变化时,如何在 Canvas 上缩放时避免出现 "jump"

windows - 我在哪里可以获得我的通用应用程序的客户端密码?

wpf - 在 xaml 标记内设置事件处理程序方法

Windows 8应用程序部署无需上传到Windows应用商店?

c# - 如何暂时禁用多语言应用程序工具包?

sqlite - 在 Windows Phone 8 中使用 SQLite 时无法使用商店测试工具包

windows-8 - 限制 Windows 8 应用程序以纵向模式查看(WinRT)