I want to place an image inside my custom control , so my generic.xaml looks like below:
<Style TargetType="local:generic">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="local:generic">
<Grid Background="{TemplateBinding Background}">
<Rectangle>
<Rectangle.Fill>
<SolidColorBrush x:Name="BackgroundBrush" Opacity="0" />
</Rectangle.Fill>
</Rectangle>
<TextBlock Text="{TemplateBinding Text}"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Foreground="{TemplateBinding Foreground}"/>
<Image Source="{TemplateBinding Source}"
HorizontalAlignment="Center"
VerticalAlignment="Center"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
My Codebehind is as follows:
public class Generic : Control
{
public static DependencyProperty ImageUri = DependencyProperty.Register("Source", typeof(Uri), typeof(generic), new PropertyMetadata(""));
public Uri Source
{
get { return (Uri)GetValue(generic.ImageUri); }
set { SetValue(generic.ImageUri, value); }
}
public generic()
{
this.DefaultStyleKey = typeof(generic);
}
}
Apllication is compiling fine but while I am trying to run it throws the following exception :
$exception
{System.Windows.Markup.XamlParseException: System.TypeInitializationException:
The type initializer for 'testCstmCntrl.themes.generic' threw an exception. ---> System.ArgumentException: Default value type does not match type of property.
Thanks,
Subhen
Now got it working , the image source is looking for BitmapImage as source . So while getting the value in our get method we have to specify Bitmap as return type.
We can return the bitmap by passing our URI from dependencyProperty registered name.
So now my code looks like below:
Thanks agn for all your suggestions and support.