WPF 样式的一个问题

code_long 2020-07-08 02:12:47
无法将类型为“MS.Internal.NamedObject”的对象强制转换为类型“System.Windows.FrameworkTemplate”。
在 System.Windows.Markup.WpfXamlLoader.Load(XamlReader xamlReader, IXamlObjectWriterFactory writerFactory, Boolean skipJournaledProperties, Object rootObject, XamlObjectWriterSettings settings, Uri baseUri)
问题应该是出现在
<Setter Property="Template" Value="{StaticResource WindowControlTemplate}"></Setter>
Template某一个属性值不对 麻烦帮忙看一下


<Style x:Key="ChildWinStyle" TargetType="{x:Type Window}">
<Setter Property="OverridesDefaultStyle" Value="True"></Setter>
<Setter Property="ResizeMode" Value="CanResizeWithGrip"></Setter>
<Setter Property="SnapsToDevicePixels" Value="true"/>
<Setter Property="FontFamily" Value="Arial"></Setter>
<Setter Property="Window.BorderBrush" Value="#FF4C95BA"></Setter>
<Setter Property="Window.BorderThickness" Value="0"></Setter>
<Setter Property="Template" Value="{StaticResource WindowControlTemplate}"></Setter>
<!--Set Label and GroupBox foreground color-->
<Style.Resources>
<Style TargetType="{x:Type Label}" >
<Setter Property="Foreground" Value="{DynamicResource FontForeground}"></Setter>
<Setter Property="VerticalContentAlignment" Value="Center"></Setter>
</Style>
<Style x:Key="lbBaseStyle" TargetType="{x:Type Label}">
<Setter Property="Height" Value="22"></Setter>
</Style>
<Style x:Key="tabControlStyle">
<Style.Resources>
<Style TargetType="{x:Type Label}" >
<Setter Property="Foreground" Value="#000000"></Setter>
<Setter Property="VerticalContentAlignment" Value="Center"></Setter>
</Style>
</Style.Resources>
</Style>
</Style.Resources>
</Style>
<ControlTemplate x:Key="WindowControlTemplate" TargetType="{x:Type Window}" >
<Border x:Name="WinControlTpl_Border" Width="{TemplateBinding Width}" Height="{TemplateBinding Height}" BorderBrush="#FF4C95BA" BorderThickness="1" Background="White" CornerRadius="4" Padding="1">
<Border.RenderTransform>
<TransformGroup>
<ScaleTransform CenterX="400" CenterY="300" />
<SkewTransform/>
<RotateTransform/>
<TranslateTransform/>
</TransformGroup>
</Border.RenderTransform>
<Grid Background="{StaticResource WindowBackground}">
<AdornerDecorator>
<ContentPresenter/>
</AdornerDecorator>
<ResizeGrip x:Name="WindowResizeGrip" HorizontalAlignment="Right" VerticalAlignment="Bottom" />
</Grid>
</Border>
<ControlTemplate.Resources>
<Storyboard x:Key="OnLoaded1">
<DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="WinControlTpl_Border" Storyboard.TargetProperty="(UIElement.Opacity)">
<SplineDoubleKeyFrame KeyTime="00:00:00" Value="0.0"/>
<SplineDoubleKeyFrame KeyTime="00:00:00.3000000" Value="1"/>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="WinControlTpl_Border" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX)">
<SplineDoubleKeyFrame KeyTime="00:00:00" Value="0.5"/>
<SplineDoubleKeyFrame KeyTime="00:00:00.1500000" Value="1"/>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="WinControlTpl_Border" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleY)">
<SplineDoubleKeyFrame KeyTime="00:00:00" Value="0.5"/>
<SplineDoubleKeyFrame KeyTime="00:00:00.1500000" Value="1"/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</ControlTemplate.Resources>
<ControlTemplate.Triggers>
<Trigger Property="ResizeMode" Value="CanResizeWithGrip">
<Setter TargetName="WindowResizeGrip" Property="Visibility" Value="Visible"/>
</Trigger>
<EventTrigger RoutedEvent="FrameworkElement.Loaded">
<BeginStoryboard Storyboard="{StaticResource OnLoaded1}"/>
</EventTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
...全文
293 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
_秦同学_ 2021-02-20
  • 打赏
  • 举报
回复
看上去样式和模板的写法并没有问题,只是 ControlTemplate 的定义在 Style 的下面,直接在 Style 中使用 StaticResource 引用 ControlTemplate 资源是引用不到的。

8,734

社区成员

发帖
与我相关
我的任务
社区描述
WPF/Silverlight相关讨论
社区管理员
  • WPF/Silverlight社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧