我想通过在ResourceDictionary
中定义的Style
将样式应用于所有FrameworkElement
对象。在一个特定的视图中,我已经包含了资源字典,并且上面有一个TextBox
,我想使用该样式。为什么我的风格没有被拾起?
因此,这里有我与身边扮演的两个定义:
<!-- STYLE USED BY ALL FRAMEWORK ELEMENTS TO DISPLAY TOOLTIP ON VALIDATION ERROR -->
<Style TargetType="{x:Type FrameworkElement}">
<Style.Triggers>
<Trigger Property="Validation.HasError"
Value="True">
<Setter Property="ToolTip"
Value="{Binding RelativeSource={RelativeSource Self}, Path=(Validation.Errors).CurrentItem.ErrorContent}" />
</Trigger>
</Style.Triggers>
</Style>
<!-- STYLE USED BY ALL TEXTBOX CONTROLS FOR VALIDATION ERROR DISPLAY -->
<Style TargetType="{x:Type TextBox}">
<Setter Property="Validation.ErrorTemplate">
<Setter.Value>
<ControlTemplate>
<DockPanel LastChildFill="True">
<Image DockPanel.Dock="Right"
Source="{StaticResource imgDisallow16}" Width="16"/>
<Border BorderBrush="Red"
BorderThickness="1">
<AdornedElementPlaceholder />
</Border>
</DockPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
书面,为Tooltip
风格不踢,如果我直接给那风格x:Key
和参考这个名字在TextBox
,它的工作原理。但为什么不是FrameworkElement
类型呢?由于TextBox
从它继承,那就是。
同样,如果我将Tooltip
触发器添加到第二种风格,那就行得通了。那个没有名字,但是目标是TextBox
。那么,为什么没有名字就工作,但第一个没有?
编辑:这里的'文本框
<TextBox Grid.Row="0"
Grid.Column="2">
<TextBox.Text>
<Binding Path="CurrentEquipment.Name"
Mode="TwoWay"
ValidatesOnNotifyDataErrors="True"
NotifyOnValidationError="True"
UpdateSourceTrigger="PropertyChanged">
<Binding.ValidationRules>
<vr:EmailValidationRule />
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
</TextBox>
它只是不喜欢工作这个。你的'FrameworkElement'风格不适用于子类(文本框,按钮或其他)。样式完全按照类型进行匹配。 – Evk