依赖属性 - 有 2 个所有者有什么意义?

发布于 2024-11-08 11:29:31 字数 495 浏览 2 评论 0 原文

如果您查看 SelectorTabItem 类,它们显然都拥有 IsSelectedProperty

static Selector()
{
    ...
    IsSelectedProperty = DependencyProperty.RegisterAttached("IsSelected", typeof(bool), typeof(Selector), ...);
    ...
}

static TabItem()
{
    IsSelectedProperty = Selector.IsSelectedProperty.AddOwner(typeof(TabItem), ...);
    ...
}

所以我想我的问题是...既然 Tabitem 包含实际的 propertychanged 逻辑,那么 IsSelectedProperty 甚至驻留在 Selector 类中的意义是什么?

If you look at Selector and TabItem classes they apparently both own the IsSelectedProperty.

static Selector()
{
    ...
    IsSelectedProperty = DependencyProperty.RegisterAttached("IsSelected", typeof(bool), typeof(Selector), ...);
    ...
}

static TabItem()
{
    IsSelectedProperty = Selector.IsSelectedProperty.AddOwner(typeof(TabItem), ...);
    ...
}

So I guess my question is... since the Tabitem contains the actual propertychanged logic, what is the point of the IsSelectedProperty even residing in the Selector class?

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

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

发布评论

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

评论(2

总攻大人 2024-11-15 11:29:31

在这种特定情况下,Selector 具有 IsSelected DP,因为有许多从它派生的控件,其项目可以选择(ComboBoxListBoxListViewTabControlDataGrid)。它们都需要能够使用 IsSelected 标记项目,因此 DP 是在它们的公共基类中声明的。就像蒂姆说的,干。

TabItem 将自身添加为所有者的另一个原因是,在 Selector 类中,IsSelected 是一个附加属性,因为您几乎可以将任何内容设置为选择器中的一项。这样,附加属性就非常容易移动。

但是,TabItem 将自身添加为所有者,这样它就不是 TabItem 上的附加属性。它还注册了一个在值更改时调用的回调方法,以便在选择或取消选择时可以执行一些操作。

是的,在 Intellisense 中输入 并查看 IsSelected 比“但是我到底如何选择一个选项呢?”更容易。并且必须四处寻找并发现您需要使用其他类的附加属性。

In this specific case, Selector has the IsSelected DP because there are a number of controls that derive from it whose items can be selected (ComboBox, ListBox, ListView, TabControl, DataGrid). They all need an ability to mark an item with IsSelected, therefore that DP is declared in their common base class. Like Tim said, DRY.

Another reason that TabItem adds itself as an owner is that in the Selector class, IsSelected is an attached property because you can have just about anything as an item in a Selector. Attached properties are pretty mobile that way.

However, TabItem adds itself as an owner such that it is not an attached property on TabItem. It also registers a callback method to be called when the value changes so that it can do a few things when selected or unselected.

And yes, it is easier to type <TabItem and see IsSelected in Intellisense rather than going, "But how the heck do I make one selected?" and have to hunt around and find that you need to use an attached property from some other class.

画骨成沙 2024-11-15 11:29:31

它基本上只是 DRY(不要重复自己)的一个实例。以下是来自 MSDN 论坛的有关该主题的主题:

DependencyProperty.AddOwner - 重点是什么

It's basically just an instance of DRY (Don't Repeat Yourself). Here's a thread from MSDN forums on the topic:

DependencyProperty.AddOwner - What's the Point

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