Programming image buttons in WPF

I want to create a Windows toolbar (something like RocketDock) that dynamically creates buttons from serialized classes, but I cannot figure out how to dynamically set the image. As an aside, how should the image be serialized (I want to serialize the image with the storage class, and not load it from the original location of the png file every 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 for inheritance 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 the code after creating the button

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

newButton.Image does not allow. 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 the link is defined as

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

    }

, . .

+2
3

, . newButton "". , CLR getter/setter (. MSDN).

, :

ImageButton.SetImage(newButton, imageSource);
0
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);

- ?

+1

It worked for me a while 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;
0
source

All Articles