如何在反序列化“设置”时更新WPF UI?目的 [英] How to update WPF UI at deserialization of "settings" object

查看:97
本文介绍了如何在反序列化“设置”时更新WPF UI?目的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标是这样做:用户选择设置文件,读取设置并相应更新UI。显然也应该可以节省。

My goal is to do this: User selects settings file, settings are read and the UI is updated correspondingly. Saving should also be possible obviously.

我的程序当前不是WPF / XAML,现在执行此操作将意味着大量重复并需要新的设置时增加了工作。

My program is currently not WPF/XAML and doing this now would mean a lot of repetition and added work when new settings are required.

所以有人告诉我WPF / XAML是必经之路,我调查了一下并喜欢它,但我仍然不确定该怎么做。 WPF / XAML的优点当然是数据绑定,但是如果我想读取整个设置文件,则可能会用新的替换旧的设置对象。我可以使WPF程序对此做出反应并根据某些给定的数据绑定更新字段吗?

So someone told me WPF/XAML was the way to go, I looked into it and liked it but I'm still not sure how to do what I want. The advantage of WPF/XAML is of course data bindings, but if I want to read a whole settings file I'd probably replace the old settings object with a new one. Can I make a WPF program react to this and update fields according to some given data bindings?

我最感兴趣的是这是否是一个好的设计,如果不是,那么-什么?

I'm mostly interested in wether this is good design and if not - what is.

推荐答案

可以。首先,您的设置对象应实现 INotifyPropertyChanged 接口。这基本上添加了一个事件,该事件在您每次调用属性设置器时都会调用。这样,绑定到非依赖项属性会同时起作用。您实际上并不需要该界面。但是,如果您希望在第一个集合(读取所有属性)后所做的更改反映在ui中,则需要该接口。

Sure you can. First of all, your settings object should implement the INotifyPropertyChangedinterface. This basically adds an event, that is called everytime you call a property setter. That way binding to non dependency properties works both ways. You don't really need that interface. But if you want your changes after the first set(where all properties are read) to be reflected in the ui, you need that interface.

我通常使用基类为此

public class PropertyChangedNotifier : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged
    {
        add { mPropertyChanged += value; }
        remove { mPropertyChanged -= value; }
    }

    protected virtual void RaisePropertyChanged(string aPropertyName)
    {
        PropertyChangedEventHandler handler = mPropertyChanged;
        if (handler != null)
        {
            var e = new PropertyChangedEventArgs(aPropertyName);
            handler(this, e);
        }
    }

    private PropertyChangedEventHandler mPropertyChanged;
}

您的设置现在应该从该类派生。

Your settings should now be derived from that class.

class MySettings : PropertyChangedNotifier
{
 public string UserName
 {
  get{return mUserName;}
  set{mUserName=value; RaisePropertyChanged("UserName");}
 }
}

现在ui,DataBinding始终与集合 DataContext 相关。

Now for the ui, DataBinding is always related to the set DataContext.

<Window
    x:Class="MyApp.MainWindow">

<StackPanel>
  <TextBox Text="{Binding UserName}"/>
</StackPanel>

</Window>

文本框将尝试从属性 UserName的当前设置数据上下文中获取其值。现在要设置DataContext,我们可以在后面的mainwindow代码中完成。

The textbox will try to get its value from the current set datacontext from the property "UserName". Now to set the DataContext we could do that in the mainwindow code behind.

public MainWindow()
{
    InitializeComponent();
    DataContext = ReadMyUserSettings();
}

如果您随时更改Datacontext,则ui会自动更新。
然后,您对文本框所做的更改将被写回到您的设置。也可以通过添加某种取消和保存工作流程来改进此设置,以便在用户单击取消时不更改您的设置。请参见 IEditableObject BindingGroup

If you change your Datacontext any time, the ui updates automatically. And your changes to the textbox will be written back to your settings. This can also be improved by adding some sort of cancel and save workflow, so that you settings are not changed if the user hit cancel. See IEditableObject and BindingGroup for that.

希望给你一个大概的想法,它是如何工作的。

Hope that gives you a rough idea, how that works.

这篇关于如何在反序列化“设置”时更新WPF UI?目的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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