1

Trying to design a custom control, but my project won't run. When I try and build, it says "Provide value on 'System.Windows.Markup.StaticResourceHolder' threw an exception. Line number '6' and line position '7'." That line and position is where I define the "TargetType" in my control.

Generic.xaml (where it "breaks")

<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:CustomControl">

    <Style TargetType="{x:Type local:MySprite}" BasedOn = "{StaticResource {x:Type Image}}">
        <Setter Property="Source" Value="MySourceSprite.png"/>
    </Style>

</ResourceDictionary>

MySprite.cs

namespace CustomControl
{
public class MySprite : Image
{
    static MySprite()
    {
        DefaultStyleKeyProperty.OverrideMetadata(typeof(MySprite), new FrameworkPropertyMetadata(typeof(MySprite)));
    }
}
}

MainWindow.xaml

<Window x:Class="CustomControl.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:CustomControl"
    xmlns:control="clr-namespace:CustomControl"
    mc:Ignorable="d"
    Title="MainWindow" Height="350" Width="525">

<Grid>
    <control:MySprite/>
</Grid>

</Window>

What "value" am I not providing?

  • Check your exception's `InnerException` for something more specific. – Kilazur Oct 12 '16 at 20:50
  • I don't see any "InnerException", the only thing I see that is close to that is "View Detail", but that is over 1000 characters long. –  Oct 12 '16 at 20:57
  • What if you just remove the BasedOn attribute? – Clemens Oct 12 '16 at 21:01
  • Ok, that worked...why did that work? If I remove that on my other button based control, it makes it invisible/unclickable. –  Oct 12 '16 at 21:07
  • Although this question is old, but for other people who have the same problem, I think the problem is due to the way in which the styles are defined. For more information check out this [question](http://stackoverflow.com/questions/9994020/provide-value-on-system-windows-markup-staticresourceholder-threw-an-exception) – Muhammad Azeez Jan 18 '17 at 19:15

1 Answers1

0

I don't know why but I just replaced StaticResources with DynamicResources ,and it stopped throwing that error. If you find any explanation or any proper solution please share it :) .

ouflak
  • 2,458
  • 10
  • 44
  • 49