<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,但它不起作用.
我真的不想重新定义模板只是为了改变这个属性.
编辑Heena的答案很接近,但我还想为明暗主题设置不同的颜色,因为我的文本框具有透明的背景颜色.
我设法通过将Foreground =“{ThemeResource TextBoxPlaceholderTextThemeBrush}”作为模板的一部分来实现这一目标(因此,换句话说,模板完全是MSDN的默认模板),然后在页面资源中指定:
<Page.Resources> <ResourceDictionary.ThemeDictionaries> <ResourceDictionary x:Key="Light"> <SolidColorBrush x:Key="TextBoxPlaceholderTextThemeBrush" Color="Blue"/> </ResourceDictionary> ... </ResourceDictionary.ThemeDictionaries> </Page.Resources>
但这现在意味着我必须在我的页面资源中为文本框放置一个巨大的ControlTemplate样式设置器,这只是默认的完全重复!
这是否与在ControlTemplate中解析TextBoxPlaceholderTextThemeBrush的方式有关?即它发现我的自定义主题词典的原因是因为ControlTemplate是在同一资源字典中定义的?
资源
从模板中的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>
更新
资源
从模板中的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>
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>
原文地址:https://www.jb51.cc/windows/363451.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。