У меня есть следующий простой ControlTemplate для подсказки в ResourceDictionary:WPF ControlTemplate: использование StaticResource приводит к исключению XML Parse во время выполнения. Зачем?
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:s="clr-namespace:System;assembly=mscorlib"
xmlns:mwt="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero"
xmlns:local="clr-namespace:SIMOCRANECMSReporting.Resources.ControlTemplates">
<ControlTemplate x:Key="CTToolTipRoundCorners" TargetType="{x:Type ToolTip}">
<mwt:SystemDropShadowChrome Color="Gray" CornerRadius="3" Name="Shdw" SnapsToDevicePixels="True">
<Border Background="#FFF6F2F2" Opacity="1" CornerRadius="3" BorderThickness="1" BorderBrush="#FFD5D1D1">
<Grid>
<ContentPresenter Margin="{TemplateBinding Padding}" DataContext="{TemplateBinding DataContext}" />
</Grid>
</Border>
</mwt:SystemDropShadowChrome>
<ControlTemplate.Triggers>
<Trigger Property="ToolTipService.HasDropShadow">
<Setter Property="mwt:SystemDropShadowChrome.Effect" TargetName="Shdw">
<Setter.Value>
<DropShadowEffect ShadowDepth="5" Opacity="0.8"/>
</Setter.Value>
</Setter>
<Setter Property="FrameworkElement.Margin" TargetName="Shdw">
<Setter.Value>
<Thickness>0,0,10,10</Thickness>
</Setter.Value>
</Setter>
<Setter Property="mwt:SystemDropShadowChrome.Color" TargetName="Shdw">
<Setter.Value>
<Color>#00878787</Color>
</Setter.Value>
</Setter>
<Trigger.Value>
<s:Boolean>True</s:Boolean>
</Trigger.Value>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
Он объединяется с другими ресурсами в App.xaml ...
<Application x:Class="SIMOCRANECMSReporting.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="MainWindow.xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/FirstFloor.ModernUI;component/Assets/ModernUI.xaml" />
<ResourceDictionary Source="/FirstFloor.ModernUI;component/Assets/ModernUI.Light.xaml"/>
<ResourceDictionary Source="/Resources/RedTextBox.xaml"/>
<ResourceDictionary Source="/Resources/RedTextBox2.xaml"/>
<ResourceDictionary Source="/Resources/ControlTemplates/CTToolTipRoundCorners.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
... и используется в секции style.trigger стиля TextBox:
<Style x:Key="RedTextBox2" TargetType="{x:Type TextBoxBase}" BasedOn="{x:Null}">
... some more xaml ...
<Style.Triggers>
<Trigger Property="Validation.HasError" Value="True">
<Setter Property="ToolTip">
<Setter.Value>
<ToolTip Template="{StaticResource ResourceKey=CTToolTipRoundCorners}"
DataContext="{Binding RelativeSource={RelativeSource Self}, Path=PlacementTarget}"
Padding="10" HasDropShadow="True">
<ItemsControl ItemsSource="{Binding Path=(Validation.Errors)}" DisplayMemberPath="ErrorContent"/>
</ToolTip>
</Setter.Value>
</Setter>
</Trigger>
</Style.Triggers>
</Style>
Использование StaticResource для ссылки на шаблон приводит к исключению:
<ToolTip Template="{StaticResource ResourceKey=CTToolTipRoundCorners}" ... />
... при использовании DynamicResource отлично работает:
<ToolTip Template="{DynamicResource ResourceKey=CTToolTipRoundCorners}" ... />
Вопрос: Может ли кто-нибудь объяснить, почему StaticResource не Работа?
SnowballTwo, спасибо очень много. Теперь он также работает со StaticResource. –