WPF中以编程方式创建图像按钮 [英] Programmatically Creating Image Button in WPF
问题描述
我想创建一个Windows工具栏(类似的RocketDock),它动态地从序列化类创建按钮,但我无法弄清楚如何动态地设置图像。
顺便说一句,应该如何一个图像序列化(我想与存储类序列化的形象,而不是从原始PNG文件的位置,每次加载它)?
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)?
我发现下面的code创建一个依赖属性。
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>
但我想不出如何设置从code中的形象后,我创建按钮
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不能解决。我需要做一些与style.Resources?
newButton.Image does not resolve. Do I need to do something with style.Resources?
谢谢,但...使用...
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);
其中的链接被定义为
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.
推荐答案
您没有创建一个普通的依赖属性,但一个的连接的依赖项属性。您newButton实例没有定义的属性图像。 Therfore你没有的便利CLR属性的getter / setter(见的 MSDN 了解详细信息)。
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).
在code应类似于此设置图像源值:
The code should look similar to this to set the image source value:
ImageButton.SetImage(newButton, imageSource);
这篇关于WPF中以编程方式创建图像按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!