我想通過在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