【问题标题】:View Default Style of Element(TextBox)查看元素的默认样式(文本框)
【发布时间】:2015-01-29 03:46:48
【问题描述】:

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

本机 TextBox 的适当颜色是什么和/或如何访问本机 TextBox 的默认样式以复制适当的语法? MSDN 上的示例似乎不是标准样式?我还看到了使用 Blend 来获取默认样式的建议?这似乎也不起作用,由于某种原因,这种方法在我的项目中创建了对 PresentationFramework.Classic 的引用,并为我提供了一个看起来像 Windows 窗体(下沉边框等)的文本框。

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>

【问题讨论】:

标签: wpf xaml custom-controls


【解决方案1】:

我在博客上找到了这个,它帮助了我。它将指定元素的模板写入控制台。它需要在创建窗口后执行,例如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 的引用?但是将不透明度添加到我现有的控件中确实修复了我原来的行为,所以我对被它弄糊涂的结果感到满意。如果有人能详细说明,我很乐意看到一些额外的 cmets。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-25
    相关资源
    最近更新 更多