我试图修改基于TextBox的CustomControl的样式,我已经基本上成功了。我试图完成的最后一件事是在禁用时使其昏暗。我已经成功地将其工作,但颜色已关闭,它们在禁用时与标准TextBox不匹配。
本机TextBox的适当颜色是什么和/或如何访问本机TextBox的默认样式以复制适当的语法? MSDN上的示例似乎不是标准样式?我还看到过使用Blend获取默认样式的建议?这似乎不起作用,由于某种原因,这种方法在我的项目中创建了对PresentationFramework.Classic的引用,并为我提供了一个看起来像Windows窗体(沉没边框等)的TextBox。
Generic.xaml
<Style x:Key="{x:Type l:CustomTextBox}" BasedOn="{StaticResource {x:Type TextBox}}" TargetType="{x:Type l:CustomTextBox}">
<Setter Property="SnapsToDevicePixels" Value="True" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type l:CustomTextBox}">
<Border x:Name="Border" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}">
<ScrollViewer x:Name="PART_ContentHost" Margin="2" />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsEnabled" Value="False">
<!-- The background does change, but the color does not match native disabled TextBoxes. -->
<Setter Property="Background" TargetName="Border" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}" />
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}" />
</Trigger>
<!-- Additional triggers, none of which modify the Background. -->
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
答案 0 :(得分:1)
我在博客上发现了这个,它帮助了我。它会将指定元素的模板写入控制台。它需要在创建窗口后执行,例如已加载的事件。
var stringBuilder = new StringBuilder();
var xmlSettings = new XmlWriterSettings();
xmlSettings.Indent = true;
using (XmlWriter xmlWriter = XmlWriter.Create(stringBuilder, xmlSettings))
XamlWriter.Save(this.textBox.Template, xmlWriter);
Console.WriteLine(stringBuilder.ToString());
<强>输出强>
<?xml version="1.0" encoding="utf-16"?>
<ControlTemplate TargetType="TextBoxBase" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:s="clr-namespace:System;assembly=mscorlib">
<Border BorderThickness="{TemplateBinding Border.BorderThickness}" BorderBrush="{TemplateBinding Border.BorderBrush}" Background="{TemplateBinding Panel.Background}" Name="border" SnapsToDevicePixels="True">
<ScrollViewer HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Hidden" Name="PART_ContentHost" Focusable="False" />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="UIElement.IsEnabled">
<Setter Property="UIElement.Opacity" TargetName="border">
<Setter.Value>
<s:Double>0.56</s:Double>
</Setter.Value>
</Setter>
<Trigger.Value>
<s:Boolean>False</s:Boolean>
</Trigger.Value>
</Trigger>
<Trigger Property="UIElement.IsMouseOver">
<Setter Property="Border.BorderBrush" TargetName="border">
<Setter.Value>
<SolidColorBrush>#FFC5DAED</SolidColorBrush>
</Setter.Value>
</Setter>
<Trigger.Value>
<s:Boolean>True</s:Boolean>
</Trigger.Value>
</Trigger>
<Trigger Property="UIElement.IsKeyboardFocused">
<Setter Property="Border.BorderBrush" TargetName="border">
<Setter.Value>
<SolidColorBrush>#FFB5CFE7</SolidColorBrush>
</Setter.Value>
</Setter>
<Trigger.Value>
<s:Boolean>True</s:Boolean>
</Trigger.Value>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
生成的模板不是我的预期,没有引用SystemColor的?但是将Opacity添加到我现有的控件确实修复了我原来的行为,所以我很满意它的结果。如果有人可以详细说明,我很乐意看到一些额外的评论。