更新绑定没有DependencyProperty [英] Update binding without DependencyProperty

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

问题描述

我有很多现有的业务对象,其中有许多属性和集合,我想绑定用户界面。在这些对象内使用 DependencyProperty ObservableCollections 不是一个选项。当我知道我什么时候修改这些对象,我想有一个机制来更新所有的UI控件,当我这样做。作为一个额外的,我也不知道哪些UI控件绑定到这些对象和什么属性。

I have a lot of existing business objects with many properties and collections inside which I want to bind the userinterface to. Using DependencyProperty or ObservableCollections inside these objects is not an option. As I know exactly when I modify these objects, I would like to have a mechanism to update all UI controls when I do this. As an extra I also don't know which UI controls bind to these objects and to what properties.

这是一个我现在试图做的简化代码:

Here is a simplified code of what I tried to do by now:

public class Artikel
{
   public int MyProperty {get;set;}
}

public partial class MainWindow : Window
{
    public Artikel artikel
    {
        get { return (Artikel)GetValue(artikelProperty); }
        set { SetValue(artikelProperty, value); }
    }

    public static readonly DependencyProperty artikelProperty =
        DependencyProperty.Register("artikel", typeof(Artikel), typeof(MainWindow), new UIPropertyMetadata(new Artikel()));

    public MainWindow()
    {
        InitializeComponent();
        test.DataContext = this;
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
         artikel.MyProperty += 1;
         // What can I do at this point to update all bindings?
         // What I know at this point is that control test or some of it's 
         // child controls bind to some property of artikel.
    }
}





<Grid Name="test">
    <TextBlock Text="{Binding Path=artikel.MyProperty}" />
</Grid>

这是,我试图将我的对象打包成一个DependencyProperty,并试图调用UpdateTarget,但是没有成功。

This is, I tried to pack my object into a DependencyProperty and tried to call UpdateTarget on this, but didn't succeed.

我可以如何更新相应的UI控件?

What could I do to update the corresponding UI controls?

我希望我描述我的情况已经够好了。

I hope I described my situation good enough.

推荐答案

使用 INotifyPropertyChanged 是DependencyProperties的一个很好的替代方案。

Using INotifyPropertyChanged is a good alternative to DependencyProperties.

如果您实现该界面,您可以提出 PropertyChanged 事件与 null 作为参数通知UI所有属性更改。

If you implement the interface you can raise the PropertyChanged event with null as parameter to notify the UI that all properties changed.

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

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