【发布时间】: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