如何创建现有控件上的依赖项属性? [英] How to create a Dependency property on an existing control?

查看:121
本文介绍了如何创建现有控件上的依赖项属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在阅读上依赖属性几天,了解他们是如何获取的价值,而不是设置/让他们在CLR性能。随时纠正我,如果我错了。

I have been reading on Dependency properties for a few days and understand how they retrieve the value rather than to set/get them as in CLR properties. Feel free to correct me if I am wrong.

从我的理解所有的WPF控件像一个TextBlock,按钮等的自DependencyObject派生也将包含依赖属性来存储,而不是使用CLR属性的值。这具有在壳体动画覆盖本地值的优点时,或者继承值,如果没有本地值被设定为所有等

From my understanding all WPF controls like a TextBlock, Button etc that derive from DependencyObject would also contain dependency properties to store their values, instead of using CLR properties. This has the advantage of overriding local values in case animations are used, or inherit values if no local value is set at all etc.

我现在想拿出一些样品创建和使用自己的DP。

I am now trying to come up with some samples to create and use my own dp.

1)是否有可能创建一个现有的WPF控件在我自己的依赖项属性?让说,我想整型的WPF的TextBlock类依赖项属性?还是我要创建,以创建上面有我的依赖属性来源于TextBlockBase一个新的类?

1) Is it possible to create my own dependency property on an existing WPF control? Let say I would like a dependency property of type integer on WPF Textblock class ? Or do i have to create a new class derived from TextBlockBase in order to create my dependency property above in there?

2)在任何情况下,我们说,我建立了一个WPF TextBlock类依赖项属性。现在我想通过标签的内容绑定到TextBlock的是依赖属性来利用它。因此,该标签会始终显示的TextBlock的DP的实际价值,不管其继承或本地设置的。

2) In either case, let say I have created a dependency property on a WPF textblock class. Now I would like to utilize it by binding the content of label to that dependency property of the TextBlock. So that the label would always show the actual value of TextBlock's dp, no matter if its inherited or set locally.

希望有人能帮助我与这两个例子... 非常感谢, 卡瓦

Hopefully someone can help me with these two examples... Many Thanks, Kave

推荐答案

您可以使用的附加属性的吧。

定义你的财产敏:


namespace WpfApplication5
{
    public class MyProperties
    {
        public static readonly System.Windows.DependencyProperty MyIntProperty;

        static MyProperties()
        {
            MyIntProperty = System.Windows.DependencyProperty.RegisterAttached(
                "MyInt", typeof(int), typeof(MyProperties));
        }

        public static void SetMyInt(System.Windows.UIElement element, int value)
        {
            element.SetValue(MyIntProperty, value);
        }

        public static int GetMyInt(System.Windows.UIElement element)
        {
            return (int)element.GetValue(MyIntProperty);
        }
    }
}

绑定标签的内容:

Bind label content:


<Window x:Class="WpfApplication5.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:WpfApplication5"
    Title="Window1" Height="300" Width="300">
    <Grid>
        <Label Margin="98,115,51,119" Content="{Binding Path=(local:MyProperties.MyInt), RelativeSource={x:Static RelativeSource.Self}}" local:MyProperties.MyInt="42"/>
    </Grid>
</Window>

这篇关于如何创建现有控件上的依赖项属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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