我已经在ButtonStyle中创建了一个图像。现在我创建了一个附属属性,以便我可以设置该图像的源。应该是直接的,但我坚持下去。
这是我缩短的ButtonStyle:
<Style x:Key="ToolBarButtonStyle" targettype="Button"> ... <Image x:Name="toolbarImage" Source="{TemplateBinding PrismExt:ImageSourceAttachable:ImageSource}" Width="48" Height="48" /> ... </Style>
这是附加的属性定义,请注意,我不知道如何修复回调,因为dependencyproperty似乎是按钮而不是图像。而Button并没有将我的图像暴露在其风格之内。这很棘手。
namespace SalesContactManagement.Infrastructure.PrismExt { public class ImgSourceAttachable { public static void SetImgSource(DependencyObject obj,string imgSource) { obj.SetValue(ImgSourceProperty,imgSource); } public static string GetImgSource(DependencyObject obj) { return obj.GetValue(ImgSourceProperty).ToString(); } // Using a DependencyProperty as the backing store for MyProperty. This enables animation,styling,binding,etc... public static readonly DependencyProperty ImgSourceProperty = DependencyProperty.Registerattached("ImgSource",typeof(string),typeof(ImgSourceAttachable),new PropertyMetadata(Callback)); private static void Callback(DependencyObject d,DependencyPropertyChangedEventArgs e) { //((Button)d).source = new BitmapImage(new Uri(Application.Current.Host.source,e.NewValue.ToString())); } } }
这是我如何在XAML中设置图像源:
<Button PrismExt:ImgSourceAttachable.ImgSource="./Images/New.png" Style="{StaticResource ToolBarButtonStyle}" />
有什么想法吗?
非常感谢,
解决方法
以下是您如何设置附加属性的风格
<Style x:Key="ToolBarButtonStyle" targettype="Button"> <Setter Property="PrismExt:ImgSourceAttachable.ImgSource" Value="./Images/New.png"/> <!--...--> </Style>
当绑定到附加的属性时,路径应该在括号内,所以尝试使用与TemplatedParent的RelativeSource绑定
<Setter Property="Template"> <Setter.Value> <ControlTemplate targettype="Button"> <Image x:Name="toolbarImage" Source="{Binding RelativeSource={RelativeSource TemplatedParent},Path=(PrismExt:ImgSourceAttachable.ImgSource)}" Width="48" Height="48"> </Image> </ControlTemplate> </Setter.Value> </Setter>
编辑:上述代码在WPF中工作,在Silverlight中,图像显示在运行时,但在设计器中出现异常。您可以使用PropertyChangedCallback中的以下代码获取映像作为解决方法
private static void Callback(DependencyObject d,DependencyPropertyChangedEventArgs e) { Button button = d as Button; Image image = GetVisualChild<Image>(button); if (image == null) { RoutedEventHandler loadedEventHandler = null; loadedEventHandler = (object sender,RoutedEventArgs ea) => { button.Loaded -= loadedEventHandler; button.ApplyTemplate(); image = GetVisualChild<Image>(button); // Here you can use the image }; button.Loaded += loadedEventHandler; } else { // Here you can use the image } } private static T GetVisualChild<T>(DependencyObject parent) where T : DependencyObject { T child = default(T); int numVisuals = VisualTreeHelper.GetChildrenCount(parent); for (int i = 0; i < numVisuals; i++) { DependencyObject v = (DependencyObject)VisualTreeHelper.GetChild(parent,i); child = v as T; if (child == null) { child = GetVisualChild<T>(v); } if (child != null) { break; } } return child; }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。