【问题标题】:WPF ControlTemplate: Using StaticResource leads to XML Parse Exception during Runtime. Why?WPF ControlTemplate:在运行时使用 StaticResource 会导致 XML 解析异常。为什么?
【发布时间】:2017-05-27 13:02:12
【问题描述】:

我在 ResourceDictionary 中为工具提示提供了以下简单的 ControlTemplate:

<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>

... 并用于 TextBox 样式的 style.trigger 部分:

<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 不起作用?

【问题讨论】:

    标签: wpf resources


    【解决方案1】:

    资源字典的包含顺序很重要。确保包含CTToolTipRoundCorners 的字典位于包含RedTextBox2 的字典之前

    查看您的 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/ControlTemplates/CTToolTipRoundCorners.xaml"/>
                <ResourceDictionary Source="/Resources/RedTextBox.xaml"/>
                <ResourceDictionary Source="/Resources/RedTextBox2.xaml"/>                
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    
    </Application.Resources>
    

    【讨论】:

    • SnowballTwo,非常感谢。现在它也适用于 StaticResource。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-13
    • 1970-01-01
    • 1970-01-01
    • 2010-09-18
    • 2014-05-04
    • 1970-01-01
    相关资源
    最近更新 更多