【问题标题】:Programmatically Creating Image Button in WPF在 WPF 中以编程方式创建图像按钮
【发布时间】:2010-11-24 20:33:45
【问题描述】:

我想创建一个从序列化类动态创建按钮的 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));

    }

图像不显示,但文本显示。按钮外观正常。

【问题讨论】:

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


    【解决方案1】:

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

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

    ImageButton.SetImage(newButton, imageSource);
    

    【讨论】:

    • 参见“编辑 - 对奥利的回应”
    • 我还必须将 DataTemplate 更改为 ControlTemplate 才能将图像用作按钮,而不是将图像放在普通按钮上。
    【解决方案2】:
    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);
    

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

    【讨论】:

      【解决方案3】:

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-10-03
        相关资源
        最近更新 更多