WPF自定义控件样式仅适用于某些计算机

时间:2013-10-02 16:25:53

标签: c# wpf custom-controls controltemplate

我一直在编写一个使用自定义AnimatedScrollViewer控件的WPF程序,继承自ScrollViewer。它具有以下默认模板:

<Style TargetType="{x:Type Controls:AnimatedScrollViewer}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Controls:AnimatedScrollViewer}">
                <Grid x:Name="PART_LayoutRoot">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="*" />
                        <RowDefinition Height="Auto" />
                    </Grid.RowDefinitions>

                    <ScrollContentPresenter x:Name="PART_ScrollContentPresenter" />

                    <Grid Grid.Row="1">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="*" />
                            <ColumnDefinition Width="Auto" />
                        </Grid.ColumnDefinitions>

                        <ScrollBar x:Name="PART_HorizontalScrollBar"
                                   Orientation="Horizontal"
                                   IsEnabled="{TemplateBinding IsPaused}"
                                   Value="{TemplateBinding HorizontalOffset}"
                                   Maximum="{TemplateBinding ScrollableWidth}"
                                   ViewportSize="{TemplateBinding ViewportWidth}"
                                   Visibility="{TemplateBinding ComputedHorizontalScrollBarVisibility}" />

                        <StackPanel Grid.Column="1">
                            <Image Source="{StaticResource LockedImage}"
                                   Visibility="{TemplateBinding IsPlaying, Converter={StaticResource boolToVisConverter}}"/>
                            <Image Source="{StaticResource UnlockedImage}"
                                   Visibility="{TemplateBinding IsPaused, Converter={StaticResource boolToVisConverter}}"/>
                        </StackPanel>
                    </Grid>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

我使用以下方法将其设置为默认样式:

[...]
public class AnimatedScrollViewer : ScrollViewer
{
    static AnimatedScrollViewer()
    {
        DefaultStyleKeyProperty.OverrideMetadata(typeof(AnimatedScrollViewer), new FrameworkPropertyMetadata(typeof(AnimatedScrollViewer)));
    }
[...]

以下Generic.xaml字典中引用了该样式,而该字典又在MergedDictionary中引用为App.xaml

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="ConvertersDictionary.xaml" />
        <ResourceDictionary Source="pack://application:,,,/JamesWright.Utils.EndpointCaller.Views;component/Themes/EndpointStyles.xaml" />
        <ResourceDictionary Source="pack://application:,,,/JamesWright.Utils.EndpointCaller.Views;component/Themes/ImageResources.xaml" />
        <ResourceDictionary Source="pack://application:,,,/JamesWright.Utils.EndpointCaller.Views;component/Themes/Resources.xaml" />
        <ResourceDictionary Source="pack://application:,,,/JamesWright.Utils.EndpointCaller.Views;component/Controls/AnimatedScrollViewer.xaml" />
    </ResourceDictionary.MergedDictionaries>
</ResourceDictionary>

真正奇怪的是,当程序在我自己的计算机和Windows 8.1虚拟机上运行时,该样式仅应用于控件,而它不应用于我的同事的计算机和团队计算机上。我知道该样式未应用,因为只有在'IsPaused'为真时才能启用关联的ScrollBar。然而,在我同事的机器上,它始终启用。此外,我在模板中指定的图像不会显示为结果。

是否有任何已知原因导致某些机器上可能无法应用Style?我已反编译应用程序,资源正在引用正确的文件。任何帮助都是值得赞赏的,因为我们整天都在拔头发:P

更新:我已将其下载到我的Windows 8笔记本电脑,并正确应用了该样式。

UPDATE#2:我尝试使用相对路径而不是pack语法,但没有成功

1 个答案:

答案 0 :(得分:0)

在阅读this question的已接受答案后,我认为问题归结为不同的系统主题,尽管我的机器和团队计算机之间的设置看起来是一样的。如果我使用Style属性显式应用样式,则会在所有计算机上观察到正确的行为。

我将进一步调查Windows主题和默认WPF控件模板之间的关系,如果我发现任何有用的内容,我会更新此答案。