试图理解 DependencyProperty [英] Trying to understand of DependencyProperty

查看:22
本文介绍了试图理解 DependencyProperty的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为 WPF 的新手,它显然具有惊人的更改、绑定、启用和以其他方式操作的能力.我正在尝试对正在发生的事情进行心理概述,并希望有人可以确认或纠正我的读数.

Being new to WPF, and its apparently amazing ability to change, bind, enable, and otherwise manipulate. I'm trying to get a mental overview of what is happening and hope some can either confirm or correct my readings.

在 WPF 之前,您有委托和事件.您可以有十几个控件都在监听(通过注册到事件),因此当事件触发时,所有其他控件将自动收到通知,并且可以按照编码方式进行操作.比如...

Before WPF, you have delegates and events. You could have a dozen controls all listening (via being registered to the event), so when the event fires, all other controls will be notified automatically and can act on however they were so coded. Such as...

从代码隐藏,你会做类似的事情

From Code Behind, you would do something like

GotFocus += MyMethodToDoSomething;

然后,签名方法

private void MyMethodToDoSomething(object sender, RoutedEventArgs e)
{
  .. do whatever
}

此外,通过使用标准的 getter/setter,setter 可以在自己的类中调用自己的方法,以在每次有人尝试获取或设置值时执行某些操作

Additionally, by using standard getter / setter, the setter can call its own methods in its own class to do something every time someone tries to get or set a value

private int someValue;
public int SomeValue
{
   get { this.DoSomeOtherThing();
         return someValue;
       }

   set { this.DoAnotherThing();
        someValue = value;
}

现在,有依赖属性和单/双向绑定.我了解(我认为)一种模拟更多只读操作的方法.

Now, there's dependency properties, and the one/two-way binding. I understand (I think) about one-way to simulate more of a read-only operation.

无论如何,通过双向绑定,依赖项会自动通知任何依赖"于源或目标更改的人,无需显式检查是否已订阅事件,框架会自动处理更改的公告到相应的控件(目标或源).

Anyhow, with two way binding, the dependencies automatically notify anyone "depending" on a change in either the source or target respectively, without an explicit check if something has subscribed to an event, the framework automatically handles the announcing of the change to the respective controls (target or source).

那么,让我用一个旧的添加/编辑保存/取消维护表单来完成这个场景.在较旧的框架中,如果有人单击添加或编辑按钮,则所有数据输入字段都将变为启用",新记录的空白数据或编辑现有数据.同时,添加/编辑按钮将被禁用,但保存/取消按钮现在将被启用.

So, let me through this scenario out with an old Add/Edit Save/Cancel maintenance form. In an older framework, if someone clicked on an add or edit button, all the data entry fields would become "enabled" with either blank data for a new record, or editing existing data. At the same time, the add/edit buttons would become disabled, but the Save/Cancel buttons would now become enabled.

同样,通过保存/取消完成后,它将禁用所有输入字段、保存/取消并重新启用添加/编辑按钮.

Likewise when finished via Save/Cancel, it would disable all the entry fields, save/cancel, and re-enable the Add/Edit buttons.

我不太明白在这种依赖属性场景下如何处理这种类型的场景(还),但我关闭了吗?我也知道你可以绑定到几乎任何东西,包括配色方案、显示/隐藏、字体等......但我正在尝试真正掌握这些东西.

I don't quite understand how such this type of scenario would be handled under this dependency property scenario (yet), but am I close? I also understand you can bind to almost anything, including color schemes, show/hide, fonts, etc... But I'm taking small steps on trying to really grasp this stuff.

谢谢.

推荐答案

发帖人要求我重新发布我的评论作为答复.乐意效劳:-)

The poster has requested that I repost my comment as an answer. Happy to oblige :-)

我也发现这本书非常有帮助:http://www.amazon.com/WPF-4-Unleashed-Adam-Nathan/dp/0672331195

Also I've found this book very helpful: http://www.amazon.com/WPF-4-Unleashed-Adam-Nathan/dp/0672331195

我自己使用 WPF 的经验涉及在我尝试让我的程序运行时在一堆不同的资源之间返回.WPF 中有如此多的东西,当您学习它时,真的很难将它们全部牢记在心.

My own experience with WPF involves going back between a bunch of different resources as I try to get my program to work. There's so much stuff in WPF it's really hard to keep it all in your head as you are learning it.

这篇关于试图理解 DependencyProperty的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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