.net - 如何在样式中使用附加属性?

标签 .net wpf silverlight dependency-properties attached-properties

我在 ButtonStyle 中创建了一个图像。现在我创建了一个附加属性,以便我可以为该图像设置源。应该直截了当,但我坚持下去。

这是我缩短的 ButtonStyle:

<Style x:Key="ToolBarButtonStyle"
        TargetType="Button">
    ...
    <Image x:Name="toolbarImage"
            Source="{TemplateBinding PrismExt:ImageSourceAttachable:ImageSource}"
            Width="48"
            Height="48" />
    ...
</Style>

这是附加的属性定义,请注意,我不知道如何修复回调,因为依赖属性似乎是按钮而不是图像。并且 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>

当绑定(bind)到附加属性时,路径应该在括号内,所以尝试使用 RelativeSourceTemplatedParent 绑定(bind)反而
<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 中 Image在运行时显示,但在设计器中失败并出现异常。您可以在 PropertyChangedCallback 中使用以下代码来获取 Image作为一种解决方法
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;
}

关于.net - 如何在样式中使用附加属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7139641/

相关文章:

c# - 重启后台 worker

c# - 如何在方法期间通过依赖注入(inject)从池中获取 DbContext 实例?

c# - 我的部分 GUI 速度很慢

WPF,将 Path.DataProperty 转换为 Segment 对象

silverlight - Windows Phone 7 中的标准转换

c# - 如何检查文件是否正在使用?

.net - c#中的System.Security.Cryptography.RNGCryptoServiceProvider类中使用的算法是什么

wpf - 当已经是英国时,日期选择器格式化为英国格式

c# - 从 Silverlight 中的文件夹加载资源 ".resx"

silverlight - Silverlight与服务器端的通信方式有哪些?