I am developing a WPF application, and have created a custom control, we will call it 'CControl'. In the xaml document where I am designing the layout of my application. I import the style with:
xmlns:my="clr-namespace:My.Controls"
and am able to use the control just fine. The problem is I want to extend the style on CControl. In the Resource Dictionary, I am able to set:
<Style TargetType="{x:Type my:CControl}">
<Setter Property="Margin" Value="5 0 5 3" />
</Style>
This applies the style to the Control, but doesn't import the style defined by CControl, so I use:
<Style TargetType="{x:Type my:CControl}" BasedOn="{StaticResource {x:Type my:CControl}}">
<Setter Property="Margin" Value="5 0 5 3" />
</Style>
The problem is when my framework tries to load the xaml I get the following exception:
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:
I have tried different ways of using BasedOn and havent gotten anything to work. Any help would be extremely useful.
Thanks
Josh
After trying for a day to get extend the custom control's style, I was finally able to get it to work. You can use a control by defining the namespace, but if you want to extend the control's style, you need to include the ResourceDictionary for the control. I ended up adding:
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/My.Project;component/Resources/CControl.xaml" />
</ResourceDictionary.MergedDictionaries>
to my ResourceDictionary in my WPF Applications Canvas / Xaml.