与我的外部数据存储的依赖属性

发布于 2024-11-25 22:19:59 字数 558 浏览 1 评论 0原文

我想向我的用户控件添加一个属性。如果我理解正确,当我想允许在其上使用数据绑定时,必须将其声明为依赖属性。依赖属性将其值存储在隐藏在 Silverlight 系统中某处的数据字段中。但我需要以属性类型以外的格式存储数据,这就是问题所在。是否可以声明依赖属性,但使用我自己的数据存储作为其值?

我问这个是有原因的。我希望允许在 xml 字符串中导入和导出数据,但在内部将它们存储在一组对象中,并能够移动这些对象。所以我想要一个 string 属性,我需要获取 xml 字符串并转换为我内部的不同数据格式,然后在保存回数据库时将其导出回字符串。

当前版本是标准属性。它连接到一对转换方法:

public string XmlData {
  get { return ExportToXML(); }
  set { ImportFromXML(value); }
}

如何将此代码转换为依赖属性(以允许数据绑定)?

更新:

AKAIF 使用依赖属性的回调很容易实现 setter。但 getter 不支持回调。

I want to add a property to my user control. If I understand it correctly, it must be declared as dependency property when I want to allow databinding to be used on it. Dependency property stores its value in a data field hidden somewhere in Silverlight system. But I need the data to be stored in a format other than type of the property, and here is the problem. Is it possible to declare a dependency property, but use my own data store for its value?

I am asking this for a reason. I want to allow to import and export data in xml string, but internally store them in a set of objects and be able to mobify these objects. So I want to have a string property, and I need to take the xml string and convert to my internal different data format, and then export it back to string when saving back to database.

The current version is a standard property. It's connected to the pair of conversion methods:

public string XmlData {
  get { return ExportToXML(); }
  set { ImportFromXML(value); }
}

How to convert this code to a dependency property (in order to allow databinding)?

Update:

AKAIF it is easy to implement the setter using the dependency property's callback. But there is no callback supported for the getter.

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

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

发布评论

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

评论(2

自由如风 2024-12-02 22:19:59

依赖对象具有已定义的类型,但与简单属性甚至更新属性的主要区别在于,可以在不调用 getter 和设置的情况下对值进行修改。

只有附加(依赖)属性才能从外部调用其静态设置器(在解析 Xaml 期间)。

DP 的 Setter/Getter 对是为了方便您避免大量转换。 SetValue 实际上被其他所有东西(故事板系统等)所使用。

这样做的结果是,您可以使用附加属性进行解析并进行一些自己的存储,但否则我认为您不走运。

A dependency object has a defined type, but the main difference to simple or even update properties is that modifications of the values can occur without the getter and setting being called.

Only Attached (dependency) properties get their static setter called externally (during the parsing of Xaml).

The Setter/Getter pair of a DP is there for your convenience to avoid lots of casting. SetValue is actually used instead by everything else (storyboard system etc).

The upshot of this is that you can hook into the parsing with an attached property and do some of your own storage, but otherwise I think you are out of luck.

七婞 2024-12-02 22:19:59

不幸的是,据我发现,我所要求的事情无法在 Silverlight 中完成。

As far as I found, unfortunately, the thing I asked for, cannot be done in Silverlight.

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