如何保存WPF UI状态? [英] How to save WPF UI state?

查看:264
本文介绍了如何保存WPF UI状态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个的TabControl 键,在其下我有一个的TreeView DataGrid中的几个要素。当我展开树和调整数据网格列,如果我再选项卡以另一个选项卡,然后回来,整个UI状态遗忘。我不得不重新展开树,并调整列。

有没有一个合理的和现有的方法来保存UI状态?让我们将这个成 -

  1. 临时(内存)和
  2. 永久(上盘)。
解决方案

要保存您可以将相关的控件绑定(宽度身高状态 IsSelected 等)属性的绑定源的属性(即视图模型的情况下MVVM的),这将自动保存您temprory状态;为永久保存可序列化的视图模型,并将其保存在磁盘上,显然加载它需要的时候。

像保存你的树视图状态,你可以有 IsExpanded IsSelected 对象属性(视图模型),其你的的TreeView 势必会喜欢这一点 -

 <风格的TargetType ={X:类型的TreeViewItem}>
    < setter属性=IsExpandedVALUE ={结合IsExpanded,模式=双向}/>
    < setter属性=IsSelectedVALUE ={结合IsSelected,模式=双向}/>
< /样式和GT;
 

您还可以使用项目设置保存应用程序的状态, 看看这个太问题 -

<一个href="http://stackoverflow.com/questions/3784477/c-sharp-approach-for-saving-user-settings-in-a-wpf-application/3784591#3784591">c# - ?方法在WPF应用程序保存用户设置

和这篇文章 - 用户设置应用

I have a TabControl and under it I have several elements like TreeView and DataGrid. When I expand the tree and resize data grid columns, if I then tab to another tab and come back, the entire UI state is forgotten. I have to re-expand the tree and resize the columns.

Is there a sensible and existing way to save the UI state? Let's divide this into -

  1. temporary (in memory) and
  2. permanent (on the disk).

解决方案

To save the state you can bind the relevant control(Width, Height, IsSelected etc.) properties to your binding source's properties(i.e. ViewModel in case of MVVM), this will automatically save your temprory state; for permanent saving you can serialize your ViewModel and save it on disk and obviously load it when required.

like for saving your tree view state you can have IsExpanded and IsSelected properties in the object(ViewModel) that your TreeView is bound to like this -

<Style TargetType="{x:Type TreeViewItem}">
    <Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}" />
    <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />
</Style>

You can also use project settings for saving state of your application, look at this SO Question -

c# - approach for saving user settings in a WPF application?

and this article - User Settings Applied

这篇关于如何保存WPF UI状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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