依赖属性陌生性

发布于 2024-07-13 21:31:03 字数 785 浏览 6 评论 0原文

我使用 ObservableCollectionDependency Properties 组合了一个 WPF 应用程序,这很酷,因为我只需向 ObservableCollection 添加一个项目即可自动显示,例如,我将集合中的对象显示为屏幕上包装面板中的框,每个框显示其标题。

因此,我想让每个项目不仅显示其 Title 加上前缀或后缀,而且似乎甚至没有使用 Dependency Object 属性。 我可以在它上面设置一个断点,但它永远不会到达。

谁能告诉我为什么,如果我将文本添加到我的传出属性中,该文本永远不会被看到? 我读到该值实际上“不是存储在对象中,而是存储在 WPF 中”,但我不明白这意味着什么。

为什么依赖对象永远不会输出文本此文本将不会被看到

public class ApplicationItem : DependencyObject
{

    public string Title
    {
        get
        {
            return (string)GetValue("this text will NOT be seen: " + TitleProperty);
        }
        set
        {
            SetValue(TitleProperty, "this text will be seen: " + value);
        }
    }
}

I've put together a WPF application using ObservableCollection and Dependency Properties which is cool because I just have to add an item to the ObservableCollection and it shows up automatically, e.g. I display the objects in the collection as boxes on the screen in a wrappanel, each box showing its Title.

So then I wanted to have each item show not only its Title plus a prefix or suffix, but the Dependency Object property doesn't even seem to be used. I can put a break point on it and it is never reached.

Can anyone enlighten me why, if I add text to my outgoing property, that that text is never seen? I have read that the value is actually "stored not in the object but in WPF" but I don't understand what that means.

Why is the text this text will NOT be seen never output by the dependency object?

public class ApplicationItem : DependencyObject
{

    public string Title
    {
        get
        {
            return (string)GetValue("this text will NOT be seen: " + TitleProperty);
        }
        set
        {
            SetValue(TitleProperty, "this text will be seen: " + value);
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

沙沙粒小 2024-07-20 21:31:03

TitleProperty 不是普通属性,而是依赖属性,因此如果您想检索 TitleProperty 的值,您必须执行以下操作:

var title = (string)GetValue(TitleProperty);

在 WPF 指南中,WPF 和绑定引擎不会调用访问依赖属性的公共属性(不必要) )。 此公共属性仅由您的隐藏代码使用。 因此,您不得在公共财产中添加代码逻辑。

但是,您可以在注册 DP 时使用 FrameworkPropertyMetadata 并提供 CoerceValueCallback 来更改设置的值。

您还可以将 IValueConverter 与您的绑定一起使用。

TitleProperty is not a normal property but a dependency property so if you want to retrieve the value of your TitleProperty you have to do :

var title = (string)GetValue(TitleProperty);

In WPF guideline, the public property to access a Dependency Property is not called by WPF and the binding engine (not necessary). This public property is only used by your code behind. So you MUST not add code logic inside your public property.

But you can use a FrameworkPropertyMetadata when you register your DP and provide a CoerceValueCallback to change the setted value.

You can also use a IValueConverter with your binding.

若无相欠,怎会相见 2024-07-20 21:31:03

我让这个对我有用:

public string Title
{
    get 
    { 
        string value = (string)GetValue(TitleProperty); 
        return value + " postfix";
    }
    set 
    { 
        SetValue(TitleProperty, "Prefix " + value); 
    }
}

是否有理由在检索值时尝试修改该值,而不是仅在设置值时修改它?

I got this to work for me:

public string Title
{
    get 
    { 
        string value = (string)GetValue(TitleProperty); 
        return value + " postfix";
    }
    set 
    { 
        SetValue(TitleProperty, "Prefix " + value); 
    }
}

Is there a reason why you are attempting to modify the value when retrieving it rather than just modifying it when the value is set?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文