WPF:样式基于另一个在单独的程序集中

Gus*_*nti 19 wpf resources styles resourcedictionary mergeddictionaries

程序集A - ResourceDictionary包含StyleA样式.
程序集B - ResourceDictionary.MergedDictionaries将程序集A中的资源合并到B.

我想在Assembly B"基于"StyleA创建一个样式.可能吗?

我正在尝试创建这种风格:

<Style x:Key="StyleB" BasedOn="{StaticResource StyleA}">
   <Setter Property="Button.Foreground" Value="Khaki"/>
</Style>
Run Code Online (Sandbox Code Playgroud)

但是如果我使用StyleB,我在运行时会得到一个XamlParseException异常:

无法将属性"Style"中的值转换为"System.Windows.Style"类型的对象.只能基于具有基本类型"IFrameworkInputElement"的目标类型的Style.标记文件'SamSeekApp; component/mainwindow.xaml'中对象'System.Windows.Controls.Button'出错

小智 40

尝试将TargetType ="{x:Type Button}"添加到"StyleB"中.

  • 因为WPF需要知道您的样式所针对的类型,所以它可以检查您覆盖的属性等.即使父(BasedOn)样式声明目标类型,您仍需要重新声明子样式中的目标类型 (5认同)