How to assign Image Uri in a custom Control.

Posted by Subhen on Stack Overflow See other posts from Stack Overflow or by Subhen
Published on 2010-04-15T11:51:28Z Indexed on 2010/04/15 12:13 UTC
Read the original article Hit count: 401

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

© Stack Overflow or respective owner

Related posts about silverlight-3.0

Related posts about Silverlight