【问题标题】:WPF custom control style only applied on certain computersWPF 自定义控件样式仅适用于某些计算机
【发布时间】:2013-10-02 16:25:53
【问题描述】:

我一直在编写一个 WPF 程序,它使用自定义的 AnimatedScrollViewercontrol,继承自 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 字典中被引用,而在App.xaml 中又被引用为MergedDictionary

<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 虚拟机上运行时,该样式只应用于控件,而在我同事的计算机和团队机器上没有应用。我知道该样式未应用,因为关联的ScrollBar 仅应在“IsPaused”为真时启用。但是,在我同事的机器上,它始终处于启用状态。此外,我在模板中指定的图像不会显示为结果。

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

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

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

【问题讨论】:

  • 您是否尝试过使用相对路径而不是 pack uri 语法? 或将包 URI 更改为
  • 我使用pack 语法,因为Generic.xaml 在不同的程序集中,尽管我可以尝试将它们放在同一个项目中。
  • 尝试将所有资源文件放在同一个项目中,并使用相对路径或 pack://application:,,,/Subfolder/ResourceFile.xaml...
  • 我明天回去工作时会试一试。谢谢! :)
  • 不幸的是,我可以确认相对路径没有区别:(

标签: c# wpf custom-controls controltemplate


【解决方案1】:

阅读this question 的已接受答案后,我认为问题出在不同的系统主题上,尽管我的机器和团队计算机之间的设置似乎相同。如果我使用 Style 属性显式应用样式,则在所有机器上都可以观察到正确的行为。

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

【讨论】:

    猜你喜欢
    • 2019-12-02
    • 2011-01-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-16
    • 2016-12-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多