2012-07-19 122 views
1

我正在开发一个WPF应用程序,并创建了一个自定义控件,我们将其称为'CControl'。在我设计应用程序布局的xaml文档中。我导入样式:在xaml中扩展自定义控件的样式

xmlns:my="clr-namespace:My.Controls" 

并且能够使用该控件就好了。问题是我想扩展CControl的风格。在资源字典,我能够设置:

 <Style TargetType="{x:Type my:CControl}"> 
      <Setter Property="Margin" Value="5 0 5 3" /> 
     </Style> 

此样式应用于控制,但不会导入由CControl定义的样式,所以我用:

 <Style TargetType="{x:Type my:CControl}" BasedOn="{StaticResource {x:Type my:CControl}}"> 
      <Setter Property="Margin" Value="5 0 5 3" /> 
     </Style> 

问题当我的框架,试图加载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 baseUri) 
    at System.Windows.Markup.WpfXamlLoader.Load(XamlReader xamlReader, Boolean skipJournaledProperties, Uri baseUri) 
    at System.Windows.Markup.XamlReader.Load(XamlReader xamlReader, ParserContext parserContext) 
    at System.Windows.Markup.XamlReader.Load(XamlReader reader) 
    at FATPOT.Whiteboard.Report.Show() in C:\...\Report.cs 
InnerException: 
    Message=Cannot find resource named 'My.Controls.CControl'. Resource names are case sensitive. 
    Source=PresentationFramework 
    StackTrace: 
     at System.Windows.StaticResourceExtension.ProvideValueInternal(IServiceProvider serviceProvider, Boolean allowDeferredReference) 
     at System.Windows.StaticResourceExtension.ProvideValue(IServiceProvider serviceProvider) 
     at MS.Internal.Xaml.Runtime.ClrObjectRuntime.CallProvideValue(MarkupExtension me, IServiceProvider serviceProvider) 
    InnerException: 

我曾尝试使用支持算法FMP的不同方式,还没有得到任何工作。任何帮助将非常有用。

感谢

乔希

回答

3

尝试每天获得延长自定义控件的样式后,我终于能够得到它的工作。您可以通过定义名称空间来使用控件,但是如果要扩展控件的样式,则需要为控件包含ResourceDictionary。我结束了:

 <ResourceDictionary.MergedDictionaries> 
      <ResourceDictionary Source="pack://application:,,,/My.Project;component/Resources/CControl.xaml" /> 
     </ResourceDictionary.MergedDictionaries> 

我的ResourceDictionary在我的WPF应用程序Canvas/Xaml。