views:

280

answers:

1

Hello,

I have created a custom control for ImageButton as

<Style TargetType="{x:Type Button}">
     <Setter Property="Template">
           <Setter.Value>
              <ControlTemplate TargetType="{x:Type Local:ImageButton}">
               <StackPanel Height="Auto" Orientation="Horizontal">
                 <Image Margin="0,0,3,0" Source="{Binding ImageSource}" />
                 <TextBlock Text="{TemplateBinding Content}" /> 
               </StackPanel>
              </ControlTemplate>
           </Setter.Value>
     </Setter>
</Style>

ImageButton class looks like

public class ImageButton : Button
    {
        public ImageButton() : base() { }

        public ImageSource ImageSource
        {
            get { return base.GetValue(ImageSourceProperty) as ImageSource; }
            set { base.SetValue(ImageSourceProperty, value); }
        }
        public static readonly DependencyProperty ImageSourceProperty =
          DependencyProperty.Register("Source", typeof(ImageSource), typeof(ImageButton));
    }

However I'm not able to bind the ImageSource to the image as: (This code is in UI Folder and image is in Resource folder)

  <Local:ImageButton x:Name="buttonBrowse1" Width="100" Margin="10,0,10,0"
 Content="Browse ..." ImageSource="../Resources/BrowseFolder.bmp"/>

But if i take a simple image it gets displayed if same source is specified. Can anyone tell me what shall be done?

+1  A: 

You need to replace the Binding in your ControlTemplate by a TemplateBinding, just as you did for the Content property:

<Image Margin="0,0,3,0" Source="{TemplateBinding ImageSource}" />

Furthermore, the definition of your DependencyProperty is not correct. The string should read ImageSource instead of just Source:

DependencyProperty.Register("ImageSource", typeof(ImageSource), ...

I do not know whether/where this name conflict causes any problems, but at least it is highly recommended to use the exact name of the actual CLR property.

EDIT: You will also have to change the TargetType of your Style to your ImageButton:

<Style TargetType="{x:Type Local:ImageButton}">
gehho
the name conflict DOES cause problems.
Rob Fonseca-Ensor
Replacing Binding with TemplateBinding produces compile time error as Could not create an instance of type 'TemplateBindingExtension'.
Archie
see my edit....
gehho
but i have to keep the look and feel of the ImageButton the same as the original button. how else can i do it?
Archie
I do not know if this is an option, but you could make the Template of your ImageButton consist of a Button with its Content property set to the current content of your ControlTemplate, like so: `<Button><Button.Content><StackPanel>...</StackPanel></Button.Content></Button>`. As another option you might have a look at the [Style.BasedOn](http://msdn.microsoft.com/en-us/library/system.windows.style.basedon.aspx) property.
gehho
I tried replacing "Template" with "Content", simple string replacement.Is that what you meant? but this time it didnt show the image.
Archie
No, this is not what I meant. I meant this: `<ControlTemplate TargetType="{x:Type Local:ImageButton}"><Button><Button.Content><StackPanel ...><Image .../><TextBlock.../></StackPanel></Button.Content></Button></ControlTemplate>` However, I do not know whether this will work.
gehho