views:

128

answers:

2

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

A: 

Your ProperyMetaData specifies an empty string "" as the default value but the property is of type Uri not String. Use new PropertyMetaData(null) instead.

Its easy to get tripped up like this because Uri properties can be defined using strings in Xaml. However the xaml parser handles the conversion of a string to a Uri for you make it appear as though it acceptable to assign a string to a Uri typed property. However its not something that will work in C# code.

AnthonyWJones
Thanx again Mr. Jones , but While I am trying to assign the image from my XAML as below, it is not displaying any image:<cstmBtn:generic Width="100" Height="50" ImageSource="/cherry.png"/>
Subhen
Also I am not sure why the PropertyMetadata() can not accept URI.
Subhen
A: 

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:

 public class generic : Control
    {
        public static DependencyProperty ImageUri = DependencyProperty.Register("Source", typeof(BitmapImage), typeof(generic), null);

        public BitmapImage Source
        {
            get {
                //return (Uri)GetValue(generic.ImageUri); 
                string strURI =(string) GetValue(generic.ImageUri); 
                return new BitmapImage(new Uri(strURI));
            }
            set { SetValue(generic.ImageUri, value); }

        }
        public generic()
        {
            this.DefaultStyleKey = typeof(generic);
        }
    }

Thanks agn for all your suggestions and support.

Subhen