rediVider
rediVider

Reputation: 1307

Programmatically Creating Image Button in WPF

I want to create a Windows Toolbar (something like RocketDock) which dynamically creates buttons from serialized classes, but I can't figure out how to dynamically set the image. As an aside, how should a image be serialized (i want to serialize the image with the storage class, instead of loading it from the original png file location each time)?

I found the following code to create a dependency property

public class ImageButton
{
    #region Image dependency property

    public static readonly DependencyProperty ImageProperty;

    public static ImageSource GetImage(DependencyObject obj)
    {
        return (ImageSource)obj.GetValue(ImageProperty);
    }

    public static void SetImage(DependencyObject obj, ImageSource value)
    {
        obj.SetValue(ImageProperty, value);
    }

    #endregion

    static ImageButton()
    {
        var metadata = new FrameworkPropertyMetadata((ImageSource)null);
        ImageProperty = DependencyProperty.RegisterAttached("Image",
                                                            typeof(ImageSource),
                                                            typeof(ImageButton), metadata);
    }
}

And i created a button style to inherit to set the image

        <Style x:Key="ImageButton" TargetType="{x:Type Button}">
        <Setter Property="HorizontalContentAlignment" Value="Stretch" />
        <Setter Property="ContentTemplate">
            <Setter.Value>
                <DataTemplate>
                    <Grid>
                        <Image Source="{Binding Path=(Extender:ImageButton.Image), RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Button}}}"
                                HorizontalAlignment="Left" Margin="8,0,0,0" Height="16" Width="16" />
                        <TextBlock Text="{TemplateBinding Content}" HorizontalAlignment="Center" />
                    </Grid>
                </DataTemplate>
            </Setter.Value>
        </Setter>
    </Style>

but I can't figure out how to set the image from code after I create the button

var newButton = new Button();
var style = (Style)FindResource("ImageButton");

newButton.Image does not resolve. Do I need to do something with style.Resources?


EDIT - response to Olli

Thanks, but... Using...

Link link = new Link(@"c:\temp\dev\ClearAllIcon.png");
    var newButton = new Button();
    newButton.Content = "bloberific";
    var style = (Style)FindResource("ImageButton");
    ImageButton.SetImage(newButton, link.IconSource);
    MainStack.Children.Add(newButton);

where link is defined as

public Link(string iconPath)
    {
        IconPath = iconPath;
        IconSource = new BitmapImage(new Uri(iconPath));

    }

the image does not show, though the text does. The button is normal looking.

Upvotes: 2

Views: 6168

Answers (3)

DiogoA.
DiogoA.

Reputation: 53

This worked for me some time ago to create a little game.

var botao = sender as Button;
Image i = new Image();


i.Source= new BitmapImage(new Uri(@"/Resources/x.png", UriKind.RelativeOrAbsolute));
botao.Content = i;

Upvotes: 0

Dean Chalk
Dean Chalk

Reputation: 20451

Link link = new Link(@"c:\temp\dev\ClearAllIcon.png");
var newButton = new Button();
newButton.Content = "bloberific";
var style = (Style)FindResource("ImageButton");
ImageButton.SetImage(newButton, link.IconSource);
MainStack.Children.Add(newButton);

you do not use the style variable - is this the problem ?

Upvotes: 1

olli-MSFT
olli-MSFT

Reputation: 2546

You did not create a normal dependency property but an attached dependency property. Your newButton instance has no defined property "Image". Therfore you do not have the convenience CLR property getter/setter (see MSDN for details).

The code should look similar to this to set the image source value:

ImageButton.SetImage(newButton, imageSource);

Upvotes: 1

Related Questions