2012-07-19 119 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。