Xaml 如何更改特定元素而不是全局元素的文本框占位符文本颜色
MSDN列出了Xaml 如何更改特定元素而不是全局元素的文本框占位符文本颜色,xaml,windows-runtime,windows-store-apps,windows-8.1,Xaml,Windows Runtime,Windows Store Apps,Windows 8.1,MSDN列出了文本框类的样式和模板。我可以通过在App.xaml中创建ResourceDictionary来覆盖这些主题资源,如下所示: <Application.Resources> <ResourceDictionary> <ResourceDictionary.ThemeDictionaries> <ResourceDictionary x:Key="Default">
文本框
类的样式和模板。我可以通过在App.xaml
中创建ResourceDictionary
来覆盖这些主题资源,如下所示:
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.ThemeDictionaries>
<ResourceDictionary x:Key="Default">
<SolidColorBrush x:Key="TextBoxPlaceholderTextThemeBrush" Color="Yellow"/>
</ResourceDictionary>
</ResourceDictionary.ThemeDictionaries>
</ResourceDictionary>
</Application.Resources>
但是现在这意味着我也必须在我的页面资源中为文本框放置一个巨大的ControlTemplate
样式设置器,它只是默认值的一个完全复制品
这与如何从控制模板中解析mebrush
有关吗?i、 e.它发现我的自定义主题词典的原因是因为ControlTemplate
是在同一个资源词典中定义的
这应该怎么做?我是否应该对textbox进行子类化,以便所有XAML都可以移动到另一个文件中(即使它只是一个textbox)?假设您使用的是MSDN textbox 资源 从模板中的Contencontrol中删除前台属性
.....
.....
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中删除前台属性
.....
......
xaml
我知道一个老问题,但我发现要编辑一两个文本框元素的占位符颜色就必须创建一个完整的文本框样式副本,这让我感到难以置信的沮丧 所以我创建了这个方法,可以根据需要在各个文本框上使用:
public static void SetPlaceholderColor(TextBox textBox, Color color)
{
var textBoxContentGrid = VisualTreeHelper.GetChild(textBox, 0) as Grid;
for (var i = 0; i < VisualTreeHelper.GetChildrenCount(textBoxContentGrid); i++)
{
var visualChild = VisualTreeHelper.GetChild(textBoxContentGrid, i);
if ((string) visualChild.GetValue(FrameworkElement.NameProperty) != "PlaceholderTextContentPresenter")
continue;
var placeHolderContentControl = visualChild as ContentControl;
if (placeHolderContentControl != null)
placeHolderContentControl.Foreground = new SolidColorBrush(color);
}
}
publicstaticvoidsetplaceholdercolor(TextBox-TextBox,Color-Color)
{
var textBoxContentGrid=visualtreeheloper.GetChild(textBox,0)作为网格;
for(var i=0;i
这也适用于密码箱,因为TextBox参数只需使用“FrameworkElement”即可轻松重放。我感觉到了您的痛苦。。。
<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>
<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>
<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>
public static void SetPlaceholderColor(TextBox textBox, Color color)
{
var textBoxContentGrid = VisualTreeHelper.GetChild(textBox, 0) as Grid;
for (var i = 0; i < VisualTreeHelper.GetChildrenCount(textBoxContentGrid); i++)
{
var visualChild = VisualTreeHelper.GetChild(textBoxContentGrid, i);
if ((string) visualChild.GetValue(FrameworkElement.NameProperty) != "PlaceholderTextContentPresenter")
continue;
var placeHolderContentControl = visualChild as ContentControl;
if (placeHolderContentControl != null)
placeHolderContentControl.Foreground = new SolidColorBrush(color);
}
}