尝试继承主题/风格并应用额外的触发器
我正在尝试使用并理解样式的 XAML 层次结构...在简单、简单的文本框中...随处可见如何根据“IsEnabled”标志设置“禁用”背景颜色。太好了,明白了。
现在,我想要另一个从 TextBox 派生的类... MyTextBox。对于此类,我有一个属性(不是依赖属性,因此我使用的是 DataTrigger)。因此,我想保留所有正在运行的正常 TextBox 操作,但现在获取新触发器以将背景颜色正确更新为其他颜色。所以,这就是我所拥有的。只是为了澄清一下,我所有的静态颜色资源都是实体画笔......
<Style TargetType="TextBox" >
<Setter Property="FontFamily" Value="Courier New" />
<Setter Property="FontSize" Value="12" />
<Setter Property="Foreground" Value="{StaticResource MyForeground}" />
<Setter Property="Background" Value="{StaticResource MyBackground}" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="TextBox">
<Border Name="Bd" BorderThickness="{TemplateBinding BorderThickness}"
BorderBrush="{TemplateBinding BorderBrush}"
Background="{TemplateBinding Background}"
SnapsToDevicePixels="true">
<ScrollViewer Name="PART_ContentHost"
Background="{TemplateBinding Background}"
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Background" Value="{StaticResource MyDisBackground}" />
<Setter TargetName="PART_ContentHost" Property="Background"
Value="MyDisBackground"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<!-- Now, my derived (or so I was hoping) style that just adds additional trigger -->
<Style TargetType="local:MyTextBox" BasedOn="{StaticResource {x:Type TextBox}}" >
<Style.Triggers>
<DataTrigger Binding="{Binding Path=IsRequired}" Value="True">
<Setter Property="Background" Value="{StaticResource RequiredBackground}" />
</DataTrigger>
</Style.Triggers>
</Style>
我是否错过了一些简单的东西?
I'm trying to work with, and understand XAML hierarchy for styles... in simple, a simple Textbox... seen all over the place for how to set the "disabled" background color based on the "IsEnabled" flag. Great, got that.
Now, I want to have another class derived from TextBox... MyTextBox. For this class, I have a property (not dependency property, so I was using DataTrigger). So, I want to keep all the normal TextBox actions that were working, but now get the new trigger to properly update the background color to some other color.. So, here is what I have. just to clarify, all my static resources for colors are SOLID BRUSHES...
<Style TargetType="TextBox" >
<Setter Property="FontFamily" Value="Courier New" />
<Setter Property="FontSize" Value="12" />
<Setter Property="Foreground" Value="{StaticResource MyForeground}" />
<Setter Property="Background" Value="{StaticResource MyBackground}" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="TextBox">
<Border Name="Bd" BorderThickness="{TemplateBinding BorderThickness}"
BorderBrush="{TemplateBinding BorderBrush}"
Background="{TemplateBinding Background}"
SnapsToDevicePixels="true">
<ScrollViewer Name="PART_ContentHost"
Background="{TemplateBinding Background}"
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Background" Value="{StaticResource MyDisBackground}" />
<Setter TargetName="PART_ContentHost" Property="Background"
Value="MyDisBackground"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<!-- Now, my derived (or so I was hoping) style that just adds additional trigger -->
<Style TargetType="local:MyTextBox" BasedOn="{StaticResource {x:Type TextBox}}" >
<Style.Triggers>
<DataTrigger Binding="{Binding Path=IsRequired}" Value="True">
<Setter Property="Background" Value="{StaticResource RequiredBackground}" />
</DataTrigger>
</Style.Triggers>
</Style>
Am I missing something simple?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,您的
DataTrigger
正在查看MyTextBox
的DataContext
(而不是控件本身)。因此,看看该控件,您需要执行以下操作:现在,当
MyTextBox.IsRequired
为 true 时,将设置MyTextBox.Background
属性。但依赖项属性值具有优先顺序。因此,上面的内容将在视觉上改变所使用的背景,例如:在以下情况下,将不会使用您的
RequiredBackground
画笔。相反,您会看到MyDisBackground
画笔:在这种情况下,
ScrollViewer.Background
更改为MyDisBackground
并且不再绑定到>MyTextBox.Background
属性。MyTextBox.Background
仍然是RequiredBackground
,但它不再在任何地方使用。最后,在以下情况下,您的
RequiredBackground
画笔将不会被使用。在这里,本地值(黄色)在优先级列表中位于#3,而样式设置器位于#8。
如果您将属性设置为默认为 false 的依赖属性,那么您可以执行以下操作:
即使 TextBox 不存在该属性,它仍然可以获得依赖属性的默认值并触发它。但由于它是为 TextBox 设置的,因此永远不会使用该触发器。
First, your
DataTrigger
is looking at theDataContext
of yourMyTextBox
(not the control itself). So look at the control, you'd need to do something like:Now that will set the
MyTextBox.Background
property whenMyTextBox.IsRequired
is true. But dependency property values have a precedence order. So the above will visually change the background used like:In the following case your
RequiredBackground
brush will not be used. Instead you'll see theMyDisBackground
brush:In this case, the
ScrollViewer.Background
is changed toMyDisBackground
and no longer binds to theMyTextBox.Background
property. TheMyTextBox.Background
would still beRequiredBackground
, but it's no longer used anywhere.Finally, in the following case your
RequiredBackground
brush will not be used.Here, the local value (yellow) is at #3 in the precedence list, while the style setter is at #8.
If you make your property a dependency property that defaults to false, then you could do something like:
Eventhough the property doesn't exist for TextBox, it can still get the default value of your dependency property and trigger off it. But since it would be set for a TextBox, that trigger will never be used.