我是 WPF 新手,有 WinForms 背景,对绑定(bind)与事件处理有一个相当基本的问题。
为了尝试保持一定的责任分离,我有一堆 Presentation
对象,它们仅具有 Dependency Properties
来保存业务对象的 UI 数据部分,业务对象包含类似的数据,但数据类型有时不同,因此 Presentation
对象对于显示目的是正确的。所以类似
public class MyPresentation
{
// bunch of dependency properties
public bool MyProperty
{
get { return (bool)GetValue(MyPropertyProperty); }
set { SetValue(MyPropertyProperty, value); }
}
// Using a DependencyProperty as the backing store for MyProperty. This enables animation, styling, binding, etc...
public static readonly DependencyProperty MyPropertyProperty =
DependencyProperty.Register("MyProperty", typeof(bool), typeof(MyPresentationObject), new UIPropertyMetadata(false, MyPresentationObject.MyPropertyPropertyChanged));
MyBusinessObject RelatedBusinessObject { get; set;}
public MyPresentation(MyBusinessObject businessObejct)
{
this.RelatedBusinessObject = businessObject;
}
public static void MyPropertyPropertyChanged()
{
// Do some stuff to related business objects
}
}
MyPresentation
的属性是绑定(bind)到各种控件的数据,我使用 Trigger
等来更改演示文稿依赖属性,这会导致 OnPropertyChanged 中的业务对象发生更改
事件
我的问题是我是否以正确的方式使用绑定(bind)?通常(在 Winforms 中)我会使用单击事件等来更改我的业务对象(或其演示版本)值,但是现在您可以使用 Binding
、Trigger
和 OnPropertyChanged
事件。
我错过了什么吗?
最佳答案
Look here这提供了模型 View View 模型模式,这允许您充分利用 WPF 的绑定(bind)和命令,而不会干扰您的业务对象(例如在业务对象上实现 INotifyPropertyChanged 等 WPF 内容)
关于c# - WPF 绑定(bind)与事件处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/505744/