我正在开发一个 WPF 应用程序,并创建了一个自定义控件,我们将其称为“CControl”。在我设计应用程序布局的 xaml 文档中。我使用以下方式导入样式:
xmlns:my="clr-namespace:My.Controls"
Run Code Online (Sandbox Code Playgroud)
并且我能够很好地使用该控件。问题是我想扩展 CControl 上的样式。在资源词典中,我可以设置:
<Style TargetType="{x:Type my:CControl}">
<Setter Property="Margin" Value="5 0 5 3" />
</Style>
Run Code Online (Sandbox Code Playgroud)
这会将样式应用于 Control,但不会导入 CControl 定义的样式,因此我使用:
<Style TargetType="{x:Type my:CControl}" BasedOn="{StaticResource {x:Type my:CControl}}">
<Setter Property="Margin" Value="5 0 5 3" />
</Style>
Run Code Online (Sandbox Code Playgroud)
问题是当我的框架尝试加载 xaml 时,出现以下异常:
System.Windows.Markup.XamlParseException occurred
Message='Provide value on 'System.Windows.StaticResourceExtension' threw an exception.' Line number '18' and line position '54'.
Source=PresentationFramework
LineNumber=18
LinePosition=54
StackTrace:
at System.Windows.Markup.XamlReader.RewrapException(Exception e, IXamlLineInfo lineInfo, Uri baseUri)
at System.Windows.Markup.WpfXamlLoader.Load(XamlReader xamlReader, IXamlObjectWriterFactory writerFactory, Boolean skipJournaledProperties, Object rootObject, XamlObjectWriterSettings settings, Uri …Run Code Online (Sandbox Code Playgroud)