2017-06-14 66 views
0

我想通过在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> 
+0

它只是不喜欢工作这个。你的'FrameworkElement'风格不适用于子类(文本框,按钮或其他)。样式完全按照类型进行匹配。 – Evk

回答

1

TextBox风格,除非你明确地立足前者对后者不延长FrameworkElement风格:

<Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource {x:Type FrameworkElement}}"> 
    ... 
</Style> 
+0

不知道我关注。这似乎是另一种方式。我希望任何基于'FrameworkElement'的东西来获取Tooltip,无论它是什么。不是_just TextBoxes_。 – DonBoitnott

+1

TextBox元素仅拾取隐式TextBox样式,但不拾取TextBox样式*和* FrameworkElement样式,除非您将前者放在后者上,正如我在答案中所解释的那样。所以你需要在隐式的FrameworkElement风格上使用隐式控制风格。 – mm8

+0

然后,我正在质疑的部分是它仍然看起来特定于'TextBox'。因此,如果我想为'ComboBox'做同样的事情,那么我必须为它定义另一种风格,即使它们都是从FrameworkElement继承而来的,风格定义也是多余的。 – DonBoitnott

相关问题