2

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.

3 Answers 3

1

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);
Sign up to request clarification or add additional context in comments.

2 Comments

See "EDIT - response to Olli"
I also had to change DataTemplate to ControlTemplate for it to use the image as the button, instead of putting the image on a normal button.
1
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 ?

Comments

0

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;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.