大佬教程收集整理的这篇文章主要介绍了xaml – 如何更改特定元素的TextBox占位符文本颜色,而不是全局,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
<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>
以上是大佬教程为你收集整理的xaml – 如何更改特定元素的TextBox占位符文本颜色,而不是全局全部内容,希望文章能够帮你解决xaml – 如何更改特定元素的TextBox占位符文本颜色,而不是全局所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。