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

发布于 2024-10-04 10:38:43 字数 2582 浏览 2 评论 0原文

我想创建一个 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);

其中链接被定义为

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

    }

图像不显示,但文本显示。该按钮看起来很正常。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

迷离° 2024-10-11 10:38:43

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

设置图像源值的代码应类似于以下内容:

ImageButton.SetImage(newButton, imageSource);

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);
旧话新听 2024-10-11 10:38:43
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 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 ?

傲性难收 2024-10-11 10:38:43

这对我前段时间创建一个小游戏很有用。

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


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

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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文