将依赖项属性添加到现有的.NET类 [英] Add Dependency Property to existing .NET class

查看:122
本文介绍了将依赖项属性添加到现有的.NET类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在WPF项目中,我有很多控件,我希望可以在其中设置各个 Margin 属性并保留其他值。因此,我想避免将完整的边距设置为新的厚度 Margin = 0,5,0,15 )。因为许多边距是由样式等设置的,但是在某些情况下,我想偏离某些控件的通用样式。

IN a WPF project I have a bunch of controls in which I would like to be able to set individual Margin properties and conserve the other values. So, I would like to avoid setting the complete margin to a new Thickness (Margin="0,5,0,15"). Because many margin's are set from styles etc. But in individual cases I would like to deviate from the generic styles for certain controls.

我想,为什么不注册几个NET类 FrameWorkElement 上的新依赖项属性(例如,仅显示MarginLeft):

I thought, why not register a couple of new dependency properties on the .NET class FrameWorkElement like so (for example only MarginLeft is shown):

public class FrameWorkElementExtensions: FrameworkElement
{
    public static readonly DependencyProperty MarginLeftProperty = DependencyProperty.Register("MarginLeft", typeof(Int16?), typeof(FrameworkElement), new PropertyMetadata(null, OnMarginLeftPropertyChanged));
    public Int16? MarginLeft
    {
        get { return (Int16?)GetValue(MarginLeftProperty); }
        set { SetValue(MarginLeftProperty, value); }
    }

    private static void OnMarginLeftPropertyChanged(object obj, DependencyPropertyChangedEventArgs e)
    {
        if (obj != null && obj is UIElement)
        {
            FrameworkElement element = (FrameworkElement)obj;

            element.Margin = new Thickness((Int16?)e.NewValue ?? 0, element.Margin.Top, element.Margin.Right, element.Margin.Bottom);
        }
    }
}

但此属性不可以在代码隐藏或XAML中使用。我能以某种方式理解它,因为这个伪类永远不会实例化。试图使其成为静态类,但您不能从FrameWorkElement派生(我需要GetValue和SetValue方法)。

But this property doesn't come available in code-behind or in XAML. I can understand it somehow, because this dummy class is never instantiated or whatsoever. Tried to make it a static class but then you can't derive from FrameWorkElement (which I need for the GetValue and SetValue methods).

我找不到任何资源在网上处理了一个更笼统的问题:您可以在现有的.NET类中添加依赖项属性吗?

I couldn't find any resource on the net that treats the more generic question: Can you add dependency properties to exiting .NET classes?

任何帮助/明智的建议都会受到赞赏。

Any help / wise advice is appreciated.

BTW:只能更改保证金(厚度)的一个组件的解决方案;)

BTW: a solution for changing only one component of a Margin (Thickness) is also appreciated ;)

推荐答案

如果要定义要在不拥有的对象上设置的属性,则要定义附加属性,在这种情况下,可以使用 RegisterAttached 方法而不是Register。另外,您将属性定义为静态的get / set方法,而不是实例属性,因为这不会在对象的实例上而是在某些未知的框架元素上进行设置。链接中的帮助主题显示了一个示例。其他评论中的链接也提供了更多信息和示例。

If you want to define a property to be set on an object that you do not own then you want to define an attached property in which case you would use the RegisterAttached method instead of Register. Also you would define the property as static get/set methods and not as an instance property since this would not be set on an instance of your object but on some unknown frameworkelement. The help topic from the link shows an example. The links in the other comments also provide more information and examples.

这篇关于将依赖项属性添加到现有的.NET类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆