查看元素的默认样式(文本框)

View Default Style of Element(TextBox)

我正在尝试修改基于TextBox 的CustomControl 的样式,我已经基本成功了。我试图完成的最后一件事是在禁用时使其变暗。我已经成功地让它工作了,但是颜色是关闭的,当它们被禁用时它与标准文本框不匹配。

原生 TextBox 的合适颜色是什么and/or如何访问原生 TextBox 的默认样式以复制合适的语法? MSDN上的例子好像不是标准的Style?我还看到了使用 Blend 获取默认样式的建议?这似乎也不起作用,出于某种原因,这种方法在我的项目中创建了对 PresentationFramework.Classic 的引用,并为我提供了一个看起来像 Windows Forms(Sunken border, etc).

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>

我在博客上找到了这个,它帮助了我。它将指定元素的模板写入控制台。它需要在创建 window 之后执行,例如Loaded 事件。

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 添加到我现有的控件中确实修复了我原来的行为,所以我对结果很满意,只是被它弄糊涂了。如果有人可以详细说明,我很乐意看到一些额外的评论。