滚动条模板中的WPF System.InvalidCastException

时间:2018-01-13 07:05:56

标签: c# wpf

我试图为水平/垂直滚动条创建样式。我尝试运行程序时收到以下错误:

  

未处理的类型' System.InvalidCastException'发生在PresentationFramework.dll

中      

附加信息:无法转换类型' MS.Internal.NamedObject'输入' System.Windows.FrameworkTemplate'。

Visual Studio只给我一个通用错误弹出窗口,而intelisense没有显示任何错误。我没有详细说明,所以我不知道问题是什么。我将我的代码与我对垂直滚动条的现有风格进行了比较,但没有什么突出的。

这是我的app.xaml:

 <Application x:Class="Animated_Icon_Maker.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:Animated_Icon_Maker"
             StartupUri="MainWindow.xaml"
             xmlns:viewModels="clr-namespace:Animated_Icon_Maker.ViewModels"
             xmlns:views="clr-namespace:IAnimated_Icon_Maker.Views">
    <Application.Resources>
        <ResourceDictionary>
            <Style x:Key="{x:Type ScrollBar}"  TargetType="{x:Type ScrollBar}">
                <Setter Property="SnapsToDevicePixels" Value="True" />
                <Setter Property="OverridesDefaultStyle" Value="true" />
                <Style.Triggers>
                    <!--<Trigger Property="Orientation" Value="Horizontal">
                        <Setter Property="Width" Value="Auto" />
                        <Setter Property="Height" Value="18" />
                        <Setter Property="Template" Value="{StaticResource HorizontalScrollBar}" />
                    </Trigger>-->
                    <Trigger Property="Orientation" Value="Vertical">
                        <Setter Property="Width" Value="18" />
                        <Setter Property="Height" Value="Auto" />
                        <Setter Property="Template" Value="{StaticResource VerticalScrollBar}" />
                    </Trigger>
                </Style.Triggers>
            </Style>

            <ControlTemplate x:Key="HorizontalScrollBar" TargetType="{x:Type ScrollBar}">

            </ControlTemplate>

            <ControlTemplate x:Key="VerticalScrollBar" TargetType="{x:Type ScrollBar}">
                <Grid Background="{x:Null}" Width="10">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="0.00001*" />
                    </Grid.RowDefinitions>
                    <Track x:Name="PART_Track" Grid.Row="0" IsDirectionReversed="true" Focusable="false">
                        <Track.Thumb>
                            <Thumb x:Name="Thumb" Background="{TemplateBinding Foreground}" Style="{DynamicResource ScrollThumbs}" />
                        </Track.Thumb>
                        <Track.IncreaseRepeatButton>
                            <RepeatButton x:Name="PageUp" Command="ScrollBar.PageDownCommand" Opacity="0" Focusable="false" />
                        </Track.IncreaseRepeatButton>
                        <Track.DecreaseRepeatButton>
                            <RepeatButton x:Name="PageDown" Command="ScrollBar.PageUpCommand" Opacity="0" Focusable="false" />
                        </Track.DecreaseRepeatButton>
                    </Track>
                </Grid>
            </ControlTemplate>


        </ResourceDictionary>
    </Application.Resources>
</Application>

这种风格直接来自Microsoft文档: docs.microsoft.com和ControlTemplate来自我的工作垂直滚动条样式。

任何想法有什么不对?

修改

如果我发表评论

<Setter Property="Template" Value="{StaticResource VerticalScrollBar}" />

异常消失,所以我至少将它缩小到我的垂直滚动条ControlTemplate。

1 个答案:

答案 0 :(得分:0)

在对DynamicResourcesStaticResources之间的差异进行一些研究后,我发现在尝试使用它们之前必须先定义StaticResources点的xaml。

因此,由于我在触发器中将模板值设置为StaticResources,因此我必须在使用它们的样式之前在xaml中定义ControlTemplates

或者我可以切换到DynamicResources,而xaml顺序不重要。