How to assign Image Uri in a custom Control.
- by Subhen
Hi,
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