以编程方式在WPF中创建图像按钮

时间:2010-11-24 20:33:45

标签: c# wpf button c#-4.0 dynamic

我想创建一个Windows工具栏(类似RocketDock),它可以从序列化类中动态创建按钮,但我无法弄清楚如何动态设置图像。 另外,如何序列化图像(我想用存储类序列化图像,而不是每次都从原始的png文件位置加载它)?

我找到了以下代码来创建依赖属性

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);
    }
}

我创建了一个按钮样式来继承以设置图像

        <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>

但在创建按钮

后,我无法弄清楚如何从代码设置图像
var newButton = new Button();
var style = (Style)FindResource("ImageButton");

newButton.Image无法解析。我是否需要对style.Resources做些什么?


编辑 - 对Olli的回应

谢谢,但......使用......

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);

其中link定义为

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

    }

图片没有显示,尽管文字有。按钮看起来很正常。

3 个答案:

答案 0 :(得分: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);

你不使用样式变量 - 这是问题吗?

答案 1 :(得分:0)

您没有创建普通的依赖项属性,而是创建附加依赖项属性。您的newButton实例没有已定义的属性“Image”。因此,您没有方便的CLR属性getter / setter(有关详细信息,请参阅MSDN)。

代码应与此类似,以设置图像源值:

ImageButton.SetImage(newButton, imageSource);

答案 2 :(得分:0)

前段时间这对我有用,可以创造一个小游戏。

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


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