翼度科技»论坛 编程开发 .net 查看内容

循序渐进介绍基于CommunityToolkit.Mvvm 和HandyControl的WPF应用端开发(6)

6

主题

6

帖子

18

积分

新手上路

Rank: 1

积分
18
在我们窗口新增、编辑状态下的时候,我们往往会根据是否修改过的痕迹-也就是脏数据状态进行跟踪,如果用户发生了数据修改,我们在用户退出窗口的时候,提供用户是否丢弃修改还是继续编辑,这样在一些重要录入时的时候,可以避免用户不小心关掉窗口,导致窗口的数据要重新录入的尴尬场景。本篇随笔介绍基于WPF开发中,窗口控件脏数据状态IsDirty的跟踪处理操作。
 1、WPF的Page页面、Window窗口对象和视图模型

MVVM是Model-View-ViewModel的简写。类似于目前比较流行的MVC、MVP设计模式,主要目的是为了分离视图(View)和模型(Model)的耦合。
对于MVVM应用中,MVVM其中包括Model、View、ViewModel三者内容。其中Page或者Window对象,都是属于视图View的概念。由于目前我们程序框架大多数情况下采用IOC的控制反转方式来调用,因此对象和接口的注入是程序开始的重要工作。
.net 中 负责依赖注入和控制反转的核心组件有两个:IServiceCollection和IServiceProvider。其中,IServiceCollection负责注册,IServiceProvider负责提供实例。在注册接口和类时,IServiceCollection提供了三种注册方法,如下所示:
  1. 1、services.AddTransient<IDictDataService, DictDataService>();  // 瞬时生命周期
  2. 2、services.AddScoped<IDictDataService, DictDataService>();<Window.InputBindings>
  3.     <KeyBinding
  4.         Key="Esc"
  5.         Command="{Binding BackCommand}"
  6.         Modifiers="" />
  7. </Window.InputBindings> // 域生命周期
  8. 3、services.AddSingleton<IDictDataService, DictDataService>();  // 全局单例生命周期
复制代码
如果使用AddTransient方法注册,IServiceProvider每次都会通过GetService方法创建一个新的实例;
如果使用AddScoped方法注册, 在同一个域(Scope)内,IServiceProvider每次都会通过GetService方法调用同一个实例,可以理解为在局部实现了单例模式;
如果使用AddSingleton方法注册, 在整个应用程序生命周期内,IServiceProvider只会创建一个实例。
了解了这几个不同的注入方式,有助于我们了解WPF的整个注入对象的生命周期,对于页面来说,由于采用了导航的方式,我们在注入的时候,采用单例的方式,对于弹出的编辑、新增、导入、批量处理的这种常规视图,我们采用用完就丢弃的AddTransient方式,而视图模型为了方便,我们也采用单件的方式构建即可。
我们在WPF程序入口的程序代码App.xaml.cs中注入相关的对象信息。
登录窗口和主窗口,采用单件注入方式,如下代码所示。
  1. // 程序导航主窗体及视图模型
  2. services.AddSingleton<INavigationWindow, MainWindow>();
  3. services.AddSingleton<ViewModels.MainWindowViewModel>();
  4. //登录窗口
  5. services.AddSingleton<LoginView, LoginView>();
复制代码
而对于我们程序的视图或者视图模型对象来做,我们不可能一一按名字插入,应该通过一种动态的方式来批量处理,也就是根据各自的接口类型/继承基类来处理即可。
  1. #region 加入视图页面和视图模型
  2. //使用动态方式加入
  3. var types = System.Reflection.Assembly.GetExecutingAssembly().DefinedTypes.Select(type => type.AsType());
  4. //视图页面对象 typeof(Page),使用单件模式,每次请求都是一样的页面
  5. var viewPageBaseType = typeof(Page);
  6. var pageClasses = types.Where(x => x != viewPageBaseType && viewPageBaseType.IsAssignableFrom(x))
  7. <Window.InputBindings>
  8.     <KeyBinding
  9.         Key="Esc"
  10.         Command="{Binding BackCommand}"
  11.         Modifiers="" />
  12. </Window.InputBindings><Window.InputBindings>
  13.     <KeyBinding
  14.         Key="Esc"
  15.         Command="{Binding BackCommand}"
  16.         Modifiers="" />
  17. </Window.InputBindings><Window.InputBindings>
  18.     <KeyBinding
  19.         Key="Esc"
  20.         Command="{Binding BackCommand}"
  21.         Modifiers="" />
  22. </Window.InputBindings><Window.InputBindings>
  23.     <KeyBinding
  24.         Key="Esc"
  25.         Command="{Binding BackCommand}"
  26.         Modifiers="" />
  27. </Window.InputBindings><Window.InputBindings>
  28.     <KeyBinding
  29.         Key="Esc"
  30.         Command="{Binding BackCommand}"
  31.         Modifiers="" />
  32. </Window.InputBindings>  .Where(x => x.IsClass && !x.IsAbstract).ToList();
  33. foreach (var page in pageClasses)
  34. {
  35. <Window.InputBindings>
  36.     <KeyBinding
  37.         Key="Esc"
  38.         Command="{Binding BackCommand}"
  39.         Modifiers="" />
  40. </Window.InputBindings>services.AddSingleton(page);
  41. }
  42. //视图模型对象 typeof(ObservableObject)
  43. var viewModelBaseType = typeof(ObservableObject);
  44. var viewModels = types.Where(x => x != viewModelBaseType && viewModelBaseType.IsAssignableFrom(x))
  45. <Window.InputBindings>
  46.     <KeyBinding
  47.         Key="Esc"
  48.         Command="{Binding BackCommand}"
  49.         Modifiers="" />
  50. </Window.InputBindings><Window.InputBindings>
  51.     <KeyBinding
  52.         Key="Esc"
  53.         Command="{Binding BackCommand}"
  54.         Modifiers="" />
  55. </Window.InputBindings><Window.InputBindings>
  56.     <KeyBinding
  57.         Key="Esc"
  58.         Command="{Binding BackCommand}"
  59.         Modifiers="" />
  60. </Window.InputBindings><Window.InputBindings>
  61.     <KeyBinding
  62.         Key="Esc"
  63.         Command="{Binding BackCommand}"
  64.         Modifiers="" />
  65. </Window.InputBindings><Window.InputBindings>
  66.     <KeyBinding
  67.         Key="Esc"
  68.         Command="{Binding BackCommand}"
  69.         Modifiers="" />
  70. </Window.InputBindings>  .Where(x => x.IsClass && !x.IsAbstract).ToList();
  71. foreach (var view in viewModels)
  72. {
  73. <Window.InputBindings>
  74.     <KeyBinding
  75.         Key="Esc"
  76.         Command="{Binding BackCommand}"
  77.         Modifiers="" />
  78. </Window.InputBindings>services.AddSingleton(view);
  79. }
  80. //窗口对象,使用 Transient 模式,每次请求都是一个新的窗体
  81. var windowBaseType = typeof(Window);
  82. var windowClasses = types.Where(x => x != windowBaseType && windowBaseType.IsAssignableFrom(x))
  83. <Window.InputBindings>
  84.     <KeyBinding
  85.         Key="Esc"
  86.         Command="{Binding BackCommand}"
  87.         Modifiers="" />
  88. </Window.InputBindings><Window.InputBindings>
  89.     <KeyBinding
  90.         Key="Esc"
  91.         Command="{Binding BackCommand}"
  92.         Modifiers="" />
  93. </Window.InputBindings><Window.InputBindings>
  94.     <KeyBinding
  95.         Key="Esc"
  96.         Command="{Binding BackCommand}"
  97.         Modifiers="" />
  98. </Window.InputBindings><Window.InputBindings>
  99.     <KeyBinding
  100.         Key="Esc"
  101.         Command="{Binding BackCommand}"
  102.         Modifiers="" />
  103. </Window.InputBindings><Window.InputBindings>
  104.     <KeyBinding
  105.         Key="Esc"
  106.         Command="{Binding BackCommand}"
  107.         Modifiers="" />
  108. </Window.InputBindings>  .Where(x => x.IsClass && !x.IsAbstract).ToList();
  109. foreach (var window in windowClasses)
  110. {
  111. <Window.InputBindings>
  112.     <KeyBinding
  113.         Key="Esc"
  114.         Command="{Binding BackCommand}"
  115.         Modifiers="" />
  116. </Window.InputBindings>services.AddTransient(window);
  117. }
  118. #endregion
复制代码
以上就是普通列表页面Page类型、视图模型ViewModel、弹出式窗口的几种注入的方式,通过接口判断和基类判断的方式,自动注入相关的对象。
2、对窗口控件的修改进行跟踪

了解了上面几种对象的注入方式,我们来进一步了解弹出式窗口对象Window的控件的修改状态跟踪。
由于WPF不能像Winform那种,通过父对象的Controls集合就可以遍历出来所有的对象,然后进行一一判断,而WPF对象没有这个属性,因此也就无法直接的对控件的修改状态进行跟踪。
那是不是没有办法对窗口下面的控件进行一一判断了呢?肯定不是,办法还是有的,就是通过内置辅助类LogicalTreeHelper,或者VisualTreeHelper的方式,由于前者是所有窗口或者页面的逻辑控件都会跟踪到,后者VisualTreeHelper只是对可视化的控件进行跟踪,因此我们这里选择LogicalTreeHelper来对控件进行遍历处理。
实现的效果,就是对应窗口编辑内容发生变化,如果用户退出,提示用户即可,如下界面效果所示。

 由于窗口元素都是继承自Visual这个wpf的基类,而这个基类又是继承于DependencyObject,如下代码所示。
  1. public abstract class Visual : DependencyObject
复制代码
而辅助类,可以通过GetChildren的方法获取对应的控件列表,接口如下所示。
  1. IEnumerable GetChildren(DependencyObject current)
复制代码
稍微封装下对控件的递归遍历处理,代码如下所示。
  1. <Window.InputBindings>
  2.     <KeyBinding
  3.         Key="Esc"
  4.         Command="{Binding BackCommand}"
  5.         Modifiers="" />
  6. </Window.InputBindings><Window.InputBindings>
  7.     <KeyBinding
  8.         Key="Esc"
  9.         Command="{Binding BackCommand}"
  10.         Modifiers="" />
  11. </Window.InputBindings>/// <summary>
  12. <Window.InputBindings>
  13.     <KeyBinding
  14.         Key="Esc"
  15.         Command="{Binding BackCommand}"
  16.         Modifiers="" />
  17. </Window.InputBindings><Window.InputBindings>
  18.     <KeyBinding
  19.         Key="Esc"
  20.         Command="{Binding BackCommand}"
  21.         Modifiers="" />
  22. </Window.InputBindings>/// 使用辅助类对窗口控件进行遍历处理
  23. <Window.InputBindings>
  24.     <KeyBinding
  25.         Key="Esc"
  26.         Command="{Binding BackCommand}"
  27.         Modifiers="" />
  28. </Window.InputBindings><Window.InputBindings>
  29.     <KeyBinding
  30.         Key="Esc"
  31.         Command="{Binding BackCommand}"
  32.         Modifiers="" />
  33. </Window.InputBindings>/// </summary>
  34. <Window.InputBindings>
  35.     <KeyBinding
  36.         Key="Esc"
  37.         Command="{Binding BackCommand}"
  38.         Modifiers="" />
  39. </Window.InputBindings><Window.InputBindings>
  40.     <KeyBinding
  41.         Key="Esc"
  42.         Command="{Binding BackCommand}"
  43.         Modifiers="" />
  44. </Window.InputBindings>/// <typeparam name="T">控件类型</typeparam>
  45. <Window.InputBindings>
  46.     <KeyBinding
  47.         Key="Esc"
  48.         Command="{Binding BackCommand}"
  49.         Modifiers="" />
  50. </Window.InputBindings><Window.InputBindings>
  51.     <KeyBinding
  52.         Key="Esc"
  53.         Command="{Binding BackCommand}"
  54.         Modifiers="" />
  55. </Window.InputBindings>/// <param name="depObj">父对象</param>
  56. <Window.InputBindings>
  57.     <KeyBinding
  58.         Key="Esc"
  59.         Command="{Binding BackCommand}"
  60.         Modifiers="" />
  61. </Window.InputBindings><Window.InputBindings>
  62.     <KeyBinding
  63.         Key="Esc"
  64.         Command="{Binding BackCommand}"
  65.         Modifiers="" />
  66. </Window.InputBindings>/// <returns></returns>
  67. <Window.InputBindings>
  68.     <KeyBinding
  69.         Key="Esc"
  70.         Command="{Binding BackCommand}"
  71.         Modifiers="" />
  72. </Window.InputBindings><Window.InputBindings>
  73.     <KeyBinding
  74.         Key="Esc"
  75.         Command="{Binding BackCommand}"
  76.         Modifiers="" />
  77. </Window.InputBindings>public static IEnumerable<T> <strong>FindLogicalChildren</strong><T>(DependencyObject depObj) where T : DependencyObject
  78. <Window.InputBindings>
  79.     <KeyBinding
  80.         Key="Esc"
  81.         Command="{Binding BackCommand}"
  82.         Modifiers="" />
  83. </Window.InputBindings><Window.InputBindings>
  84.     <KeyBinding
  85.         Key="Esc"
  86.         Command="{Binding BackCommand}"
  87.         Modifiers="" />
  88. </Window.InputBindings>{
  89. <Window.InputBindings>
  90.     <KeyBinding
  91.         Key="Esc"
  92.         Command="{Binding BackCommand}"
  93.         Modifiers="" />
  94. </Window.InputBindings><Window.InputBindings>
  95.     <KeyBinding
  96.         Key="Esc"
  97.         Command="{Binding BackCommand}"
  98.         Modifiers="" />
  99. </Window.InputBindings><Window.InputBindings>
  100.     <KeyBinding
  101.         Key="Esc"
  102.         Command="{Binding BackCommand}"
  103.         Modifiers="" />
  104. </Window.InputBindings>if (depObj != null)
  105. <Window.InputBindings>
  106.     <KeyBinding
  107.         Key="Esc"
  108.         Command="{Binding BackCommand}"
  109.         Modifiers="" />
  110. </Window.InputBindings><Window.InputBindings>
  111.     <KeyBinding
  112.         Key="Esc"
  113.         Command="{Binding BackCommand}"
  114.         Modifiers="" />
  115. </Window.InputBindings><Window.InputBindings>
  116.     <KeyBinding
  117.         Key="Esc"
  118.         Command="{Binding BackCommand}"
  119.         Modifiers="" />
  120. </Window.InputBindings>{
  121. <Window.InputBindings>
  122.     <KeyBinding
  123.         Key="Esc"
  124.         Command="{Binding BackCommand}"
  125.         Modifiers="" />
  126. </Window.InputBindings><Window.InputBindings>
  127.     <KeyBinding
  128.         Key="Esc"
  129.         Command="{Binding BackCommand}"
  130.         Modifiers="" />
  131. </Window.InputBindings><Window.InputBindings>
  132.     <KeyBinding
  133.         Key="Esc"
  134.         Command="{Binding BackCommand}"
  135.         Modifiers="" />
  136. </Window.InputBindings><Window.InputBindings>
  137.     <KeyBinding
  138.         Key="Esc"
  139.         Command="{Binding BackCommand}"
  140.         Modifiers="" />
  141. </Window.InputBindings>foreach (object rawChild in LogicalTreeHelper.GetChildren(depObj))
  142. <Window.InputBindings>
  143.     <KeyBinding
  144.         Key="Esc"
  145.         Command="{Binding BackCommand}"
  146.         Modifiers="" />
  147. </Window.InputBindings><Window.InputBindings>
  148.     <KeyBinding
  149.         Key="Esc"
  150.         Command="{Binding BackCommand}"
  151.         Modifiers="" />
  152. </Window.InputBindings><Window.InputBindings>
  153.     <KeyBinding
  154.         Key="Esc"
  155.         Command="{Binding BackCommand}"
  156.         Modifiers="" />
  157. </Window.InputBindings><Window.InputBindings>
  158.     <KeyBinding
  159.         Key="Esc"
  160.         Command="{Binding BackCommand}"
  161.         Modifiers="" />
  162. </Window.InputBindings>{
  163. <Window.InputBindings>
  164.     <KeyBinding
  165.         Key="Esc"
  166.         Command="{Binding BackCommand}"
  167.         Modifiers="" />
  168. </Window.InputBindings><Window.InputBindings>
  169.     <KeyBinding
  170.         Key="Esc"
  171.         Command="{Binding BackCommand}"
  172.         Modifiers="" />
  173. </Window.InputBindings><Window.InputBindings>
  174.     <KeyBinding
  175.         Key="Esc"
  176.         Command="{Binding BackCommand}"
  177.         Modifiers="" />
  178. </Window.InputBindings><Window.InputBindings>
  179.     <KeyBinding
  180.         Key="Esc"
  181.         Command="{Binding BackCommand}"
  182.         Modifiers="" />
  183. </Window.InputBindings><Window.InputBindings>
  184.     <KeyBinding
  185.         Key="Esc"
  186.         Command="{Binding BackCommand}"
  187.         Modifiers="" />
  188. </Window.InputBindings>if (rawChild is DependencyObject)
  189. <Window.InputBindings>
  190.     <KeyBinding
  191.         Key="Esc"
  192.         Command="{Binding BackCommand}"
  193.         Modifiers="" />
  194. </Window.InputBindings><Window.InputBindings>
  195.     <KeyBinding
  196.         Key="Esc"
  197.         Command="{Binding BackCommand}"
  198.         Modifiers="" />
  199. </Window.InputBindings><Window.InputBindings>
  200.     <KeyBinding
  201.         Key="Esc"
  202.         Command="{Binding BackCommand}"
  203.         Modifiers="" />
  204. </Window.InputBindings><Window.InputBindings>
  205.     <KeyBinding
  206.         Key="Esc"
  207.         Command="{Binding BackCommand}"
  208.         Modifiers="" />
  209. </Window.InputBindings><Window.InputBindings>
  210.     <KeyBinding
  211.         Key="Esc"
  212.         Command="{Binding BackCommand}"
  213.         Modifiers="" />
  214. </Window.InputBindings>{
  215. <Window.InputBindings>
  216.     <KeyBinding
  217.         Key="Esc"
  218.         Command="{Binding BackCommand}"
  219.         Modifiers="" />
  220. </Window.InputBindings><Window.InputBindings>
  221.     <KeyBinding
  222.         Key="Esc"
  223.         Command="{Binding BackCommand}"
  224.         Modifiers="" />
  225. </Window.InputBindings><Window.InputBindings>
  226.     <KeyBinding
  227.         Key="Esc"
  228.         Command="{Binding BackCommand}"
  229.         Modifiers="" />
  230. </Window.InputBindings><Window.InputBindings>
  231.     <KeyBinding
  232.         Key="Esc"
  233.         Command="{Binding BackCommand}"
  234.         Modifiers="" />
  235. </Window.InputBindings><Window.InputBindings>
  236.     <KeyBinding
  237.         Key="Esc"
  238.         Command="{Binding BackCommand}"
  239.         Modifiers="" />
  240. </Window.InputBindings><Window.InputBindings>
  241.     <KeyBinding
  242.         Key="Esc"
  243.         Command="{Binding BackCommand}"
  244.         Modifiers="" />
  245. </Window.InputBindings>DependencyObject child = (DependencyObject)rawChild;
  246. <Window.InputBindings>
  247.     <KeyBinding
  248.         Key="Esc"
  249.         Command="{Binding BackCommand}"
  250.         Modifiers="" />
  251. </Window.InputBindings><Window.InputBindings>
  252.     <KeyBinding
  253.         Key="Esc"
  254.         Command="{Binding BackCommand}"
  255.         Modifiers="" />
  256. </Window.InputBindings><Window.InputBindings>
  257.     <KeyBinding
  258.         Key="Esc"
  259.         Command="{Binding BackCommand}"
  260.         Modifiers="" />
  261. </Window.InputBindings><Window.InputBindings>
  262.     <KeyBinding
  263.         Key="Esc"
  264.         Command="{Binding BackCommand}"
  265.         Modifiers="" />
  266. </Window.InputBindings><Window.InputBindings>
  267.     <KeyBinding
  268.         Key="Esc"
  269.         Command="{Binding BackCommand}"
  270.         Modifiers="" />
  271. </Window.InputBindings><Window.InputBindings>
  272.     <KeyBinding
  273.         Key="Esc"
  274.         Command="{Binding BackCommand}"
  275.         Modifiers="" />
  276. </Window.InputBindings>if (child is T)
  277. <Window.InputBindings>
  278.     <KeyBinding
  279.         Key="Esc"
  280.         Command="{Binding BackCommand}"
  281.         Modifiers="" />
  282. </Window.InputBindings><Window.InputBindings>
  283.     <KeyBinding
  284.         Key="Esc"
  285.         Command="{Binding BackCommand}"
  286.         Modifiers="" />
  287. </Window.InputBindings><Window.InputBindings>
  288.     <KeyBinding
  289.         Key="Esc"
  290.         Command="{Binding BackCommand}"
  291.         Modifiers="" />
  292. </Window.InputBindings><Window.InputBindings>
  293.     <KeyBinding
  294.         Key="Esc"
  295.         Command="{Binding BackCommand}"
  296.         Modifiers="" />
  297. </Window.InputBindings><Window.InputBindings>
  298.     <KeyBinding
  299.         Key="Esc"
  300.         Command="{Binding BackCommand}"
  301.         Modifiers="" />
  302. </Window.InputBindings><Window.InputBindings>
  303.     <KeyBinding
  304.         Key="Esc"
  305.         Command="{Binding BackCommand}"
  306.         Modifiers="" />
  307. </Window.InputBindings>{
  308. <Window.InputBindings>
  309.     <KeyBinding
  310.         Key="Esc"
  311.         Command="{Binding BackCommand}"
  312.         Modifiers="" />
  313. </Window.InputBindings><Window.InputBindings>
  314.     <KeyBinding
  315.         Key="Esc"
  316.         Command="{Binding BackCommand}"
  317.         Modifiers="" />
  318. </Window.InputBindings><Window.InputBindings>
  319.     <KeyBinding
  320.         Key="Esc"
  321.         Command="{Binding BackCommand}"
  322.         Modifiers="" />
  323. </Window.InputBindings><Window.InputBindings>
  324.     <KeyBinding
  325.         Key="Esc"
  326.         Command="{Binding BackCommand}"
  327.         Modifiers="" />
  328. </Window.InputBindings><Window.InputBindings>
  329.     <KeyBinding
  330.         Key="Esc"
  331.         Command="{Binding BackCommand}"
  332.         Modifiers="" />
  333. </Window.InputBindings><Window.InputBindings>
  334.     <KeyBinding
  335.         Key="Esc"
  336.         Command="{Binding BackCommand}"
  337.         Modifiers="" />
  338. </Window.InputBindings><Window.InputBindings>
  339.     <KeyBinding
  340.         Key="Esc"
  341.         Command="{Binding BackCommand}"
  342.         Modifiers="" />
  343. </Window.InputBindings>yield return (T)child;
  344. <Window.InputBindings>
  345.     <KeyBinding
  346.         Key="Esc"
  347.         Command="{Binding BackCommand}"
  348.         Modifiers="" />
  349. </Window.InputBindings><Window.InputBindings>
  350.     <KeyBinding
  351.         Key="Esc"
  352.         Command="{Binding BackCommand}"
  353.         Modifiers="" />
  354. </Window.InputBindings><Window.InputBindings>
  355.     <KeyBinding
  356.         Key="Esc"
  357.         Command="{Binding BackCommand}"
  358.         Modifiers="" />
  359. </Window.InputBindings><Window.InputBindings>
  360.     <KeyBinding
  361.         Key="Esc"
  362.         Command="{Binding BackCommand}"
  363.         Modifiers="" />
  364. </Window.InputBindings><Window.InputBindings>
  365.     <KeyBinding
  366.         Key="Esc"
  367.         Command="{Binding BackCommand}"
  368.         Modifiers="" />
  369. </Window.InputBindings><Window.InputBindings>
  370.     <KeyBinding
  371.         Key="Esc"
  372.         Command="{Binding BackCommand}"
  373.         Modifiers="" />
  374. </Window.InputBindings>}
  375. <Window.InputBindings>
  376.     <KeyBinding
  377.         Key="Esc"
  378.         Command="{Binding BackCommand}"
  379.         Modifiers="" />
  380. </Window.InputBindings><Window.InputBindings>
  381.     <KeyBinding
  382.         Key="Esc"
  383.         Command="{Binding BackCommand}"
  384.         Modifiers="" />
  385. </Window.InputBindings><Window.InputBindings>
  386.     <KeyBinding
  387.         Key="Esc"
  388.         Command="{Binding BackCommand}"
  389.         Modifiers="" />
  390. </Window.InputBindings><Window.InputBindings>
  391.     <KeyBinding
  392.         Key="Esc"
  393.         Command="{Binding BackCommand}"
  394.         Modifiers="" />
  395. </Window.InputBindings><Window.InputBindings>
  396.     <KeyBinding
  397.         Key="Esc"
  398.         Command="{Binding BackCommand}"
  399.         Modifiers="" />
  400. </Window.InputBindings><Window.InputBindings>
  401.     <KeyBinding
  402.         Key="Esc"
  403.         Command="{Binding BackCommand}"
  404.         Modifiers="" />
  405. </Window.InputBindings>foreach (T childOfChild in <strong>FindLogicalChildren</strong><T>(child))
  406. <Window.InputBindings>
  407.     <KeyBinding
  408.         Key="Esc"
  409.         Command="{Binding BackCommand}"
  410.         Modifiers="" />
  411. </Window.InputBindings><Window.InputBindings>
  412.     <KeyBinding
  413.         Key="Esc"
  414.         Command="{Binding BackCommand}"
  415.         Modifiers="" />
  416. </Window.InputBindings><Window.InputBindings>
  417.     <KeyBinding
  418.         Key="Esc"
  419.         Command="{Binding BackCommand}"
  420.         Modifiers="" />
  421. </Window.InputBindings><Window.InputBindings>
  422.     <KeyBinding
  423.         Key="Esc"
  424.         Command="{Binding BackCommand}"
  425.         Modifiers="" />
  426. </Window.InputBindings><Window.InputBindings>
  427.     <KeyBinding
  428.         Key="Esc"
  429.         Command="{Binding BackCommand}"
  430.         Modifiers="" />
  431. </Window.InputBindings><Window.InputBindings>
  432.     <KeyBinding
  433.         Key="Esc"
  434.         Command="{Binding BackCommand}"
  435.         Modifiers="" />
  436. </Window.InputBindings>{
  437. <Window.InputBindings>
  438.     <KeyBinding
  439.         Key="Esc"
  440.         Command="{Binding BackCommand}"
  441.         Modifiers="" />
  442. </Window.InputBindings><Window.InputBindings>
  443.     <KeyBinding
  444.         Key="Esc"
  445.         Command="{Binding BackCommand}"
  446.         Modifiers="" />
  447. </Window.InputBindings><Window.InputBindings>
  448.     <KeyBinding
  449.         Key="Esc"
  450.         Command="{Binding BackCommand}"
  451.         Modifiers="" />
  452. </Window.InputBindings><Window.InputBindings>
  453.     <KeyBinding
  454.         Key="Esc"
  455.         Command="{Binding BackCommand}"
  456.         Modifiers="" />
  457. </Window.InputBindings><Window.InputBindings>
  458.     <KeyBinding
  459.         Key="Esc"
  460.         Command="{Binding BackCommand}"
  461.         Modifiers="" />
  462. </Window.InputBindings><Window.InputBindings>
  463.     <KeyBinding
  464.         Key="Esc"
  465.         Command="{Binding BackCommand}"
  466.         Modifiers="" />
  467. </Window.InputBindings><Window.InputBindings>
  468.     <KeyBinding
  469.         Key="Esc"
  470.         Command="{Binding BackCommand}"
  471.         Modifiers="" />
  472. </Window.InputBindings>yield return childOfChild;
  473. <Window.InputBindings>
  474.     <KeyBinding
  475.         Key="Esc"
  476.         Command="{Binding BackCommand}"
  477.         Modifiers="" />
  478. </Window.InputBindings><Window.InputBindings>
  479.     <KeyBinding
  480.         Key="Esc"
  481.         Command="{Binding BackCommand}"
  482.         Modifiers="" />
  483. </Window.InputBindings><Window.InputBindings>
  484.     <KeyBinding
  485.         Key="Esc"
  486.         Command="{Binding BackCommand}"
  487.         Modifiers="" />
  488. </Window.InputBindings><Window.InputBindings>
  489.     <KeyBinding
  490.         Key="Esc"
  491.         Command="{Binding BackCommand}"
  492.         Modifiers="" />
  493. </Window.InputBindings><Window.InputBindings>
  494.     <KeyBinding
  495.         Key="Esc"
  496.         Command="{Binding BackCommand}"
  497.         Modifiers="" />
  498. </Window.InputBindings><Window.InputBindings>
  499.     <KeyBinding
  500.         Key="Esc"
  501.         Command="{Binding BackCommand}"
  502.         Modifiers="" />
  503. </Window.InputBindings>}
  504. <Window.InputBindings>
  505.     <KeyBinding
  506.         Key="Esc"
  507.         Command="{Binding BackCommand}"
  508.         Modifiers="" />
  509. </Window.InputBindings><Window.InputBindings>
  510.     <KeyBinding
  511.         Key="Esc"
  512.         Command="{Binding BackCommand}"
  513.         Modifiers="" />
  514. </Window.InputBindings><Window.InputBindings>
  515.     <KeyBinding
  516.         Key="Esc"
  517.         Command="{Binding BackCommand}"
  518.         Modifiers="" />
  519. </Window.InputBindings><Window.InputBindings>
  520.     <KeyBinding
  521.         Key="Esc"
  522.         Command="{Binding BackCommand}"
  523.         Modifiers="" />
  524. </Window.InputBindings><Window.InputBindings>
  525.     <KeyBinding
  526.         Key="Esc"
  527.         Command="{Binding BackCommand}"
  528.         Modifiers="" />
  529. </Window.InputBindings>}
  530. <Window.InputBindings>
  531.     <KeyBinding
  532.         Key="Esc"
  533.         Command="{Binding BackCommand}"
  534.         Modifiers="" />
  535. </Window.InputBindings><Window.InputBindings>
  536.     <KeyBinding
  537.         Key="Esc"
  538.         Command="{Binding BackCommand}"
  539.         Modifiers="" />
  540. </Window.InputBindings><Window.InputBindings>
  541.     <KeyBinding
  542.         Key="Esc"
  543.         Command="{Binding BackCommand}"
  544.         Modifiers="" />
  545. </Window.InputBindings><Window.InputBindings>
  546.     <KeyBinding
  547.         Key="Esc"
  548.         Command="{Binding BackCommand}"
  549.         Modifiers="" />
  550. </Window.InputBindings>}
  551. <Window.InputBindings>
  552.     <KeyBinding
  553.         Key="Esc"
  554.         Command="{Binding BackCommand}"
  555.         Modifiers="" />
  556. </Window.InputBindings><Window.InputBindings>
  557.     <KeyBinding
  558.         Key="Esc"
  559.         Command="{Binding BackCommand}"
  560.         Modifiers="" />
  561. </Window.InputBindings><Window.InputBindings>
  562.     <KeyBinding
  563.         Key="Esc"
  564.         Command="{Binding BackCommand}"
  565.         Modifiers="" />
  566. </Window.InputBindings>}
  567. <Window.InputBindings>
  568.     <KeyBinding
  569.         Key="Esc"
  570.         Command="{Binding BackCommand}"
  571.         Modifiers="" />
  572. </Window.InputBindings><Window.InputBindings>
  573.     <KeyBinding
  574.         Key="Esc"
  575.         Command="{Binding BackCommand}"
  576.         Modifiers="" />
  577. </Window.InputBindings>}
复制代码
这样我们如果需要获取父控件类下面所有的TextBox控件列表,只需要如下操作即可。
  1. //文本控件
  2. var texboxList = <strong>FindLogicalChildren</strong><TextBoxBase>(depObj);
  3. foreach (var textbox in texboxList)
  4. {
  5. <Window.InputBindings>
  6.     <KeyBinding
  7.         Key="Esc"
  8.         Command="{Binding BackCommand}"
  9.         Modifiers="" />
  10. </Window.InputBindings>if (textbox != null && !textbox.IsReadOnly)
  11. <Window.InputBindings>
  12.     <KeyBinding
  13.         Key="Esc"
  14.         Command="{Binding BackCommand}"
  15.         Modifiers="" />
  16. </Window.InputBindings>{
  17. <Window.InputBindings>
  18.     <KeyBinding
  19.         Key="Esc"
  20.         Command="{Binding BackCommand}"
  21.         Modifiers="" />
  22. </Window.InputBindings><Window.InputBindings>
  23.     <KeyBinding
  24.         Key="Esc"
  25.         Command="{Binding BackCommand}"
  26.         Modifiers="" />
  27. </Window.InputBindings>textbox.TextChanged += (s, e) => { <strong>MainModelHelper.SetIsDirty</strong>(true); };//文本变化触发
  28. <Window.InputBindings>
  29.     <KeyBinding
  30.         Key="Esc"
  31.         Command="{Binding BackCommand}"
  32.         Modifiers="" />
  33. </Window.InputBindings>}
  34. }
复制代码
其他控件也是类似的方式处理,例如对于CheckBox和RadioButton,可以对它们共同的基类进行一并处理,如下所示。
  1. //ToggleButton,包含CheckBox、RadioButton
  2. var buttonList = FindLogicalChildren<ToggleButton>(depObj);
  3. foreach (var toggle in buttonList)
  4. {
  5. <Window.InputBindings>
  6.     <KeyBinding
  7.         Key="Esc"
  8.         Command="{Binding BackCommand}"
  9.         Modifiers="" />
  10. </Window.InputBindings>if (toggle != null && toggle.IsEnabled)
  11. <Window.InputBindings>
  12.     <KeyBinding
  13.         Key="Esc"
  14.         Command="{Binding BackCommand}"
  15.         Modifiers="" />
  16. </Window.InputBindings>{
  17. <Window.InputBindings>
  18.     <KeyBinding
  19.         Key="Esc"
  20.         Command="{Binding BackCommand}"
  21.         Modifiers="" />
  22. </Window.InputBindings><Window.InputBindings>
  23.     <KeyBinding
  24.         Key="Esc"
  25.         Command="{Binding BackCommand}"
  26.         Modifiers="" />
  27. </Window.InputBindings>toggle.Checked += (s, e) => { MainModelHelper.SetIsDirty(true); };//选择变化触发
  28. <Window.InputBindings>
  29.     <KeyBinding
  30.         Key="Esc"
  31.         Command="{Binding BackCommand}"
  32.         Modifiers="" />
  33. </Window.InputBindings><Window.InputBindings>
  34.     <KeyBinding
  35.         Key="Esc"
  36.         Command="{Binding BackCommand}"
  37.         Modifiers="" />
  38. </Window.InputBindings>toggle.Unchecked += (s, e) => { MainModelHelper.SetIsDirty(true); };//选择变化触发
  39. <Window.InputBindings>
  40.     <KeyBinding
  41.         Key="Esc"
  42.         Command="{Binding BackCommand}"
  43.         Modifiers="" />
  44. </Window.InputBindings>}
  45. }
复制代码
这样我们把它放到一个静态的辅助类里面方便使用,如下所示。
  1. /// <summary>
  2. /// 对WPF控件的相关处理,包括遍历查找等
  3. /// </summary>
  4. public static class ControlHelper
  5. {
  6. <Window.InputBindings>
  7.     <KeyBinding
  8.         Key="Esc"
  9.         Command="{Binding BackCommand}"
  10.         Modifiers="" />
  11. </Window.InputBindings>/// <summary>
  12. <Window.InputBindings>
  13.     <KeyBinding
  14.         Key="Esc"
  15.         Command="{Binding BackCommand}"
  16.         Modifiers="" />
  17. </Window.InputBindings>/// 对界面的控件遍历,并监测状态变化
  18. <Window.InputBindings>
  19.     <KeyBinding
  20.         Key="Esc"
  21.         Command="{Binding BackCommand}"
  22.         Modifiers="" />
  23. </Window.InputBindings>/// </summary>
  24. <Window.InputBindings>
  25.     <KeyBinding
  26.         Key="Esc"
  27.         Command="{Binding BackCommand}"
  28.         Modifiers="" />
  29. </Window.InputBindings>/// <param name="depObj">父节点控件</param>
  30. <Window.InputBindings>
  31.     <KeyBinding
  32.         Key="Esc"
  33.         Command="{Binding BackCommand}"
  34.         Modifiers="" />
  35. </Window.InputBindings>public static void SetDirtyEvent(DependencyObject depObj)
  36. <Window.InputBindings>
  37.     <KeyBinding
  38.         Key="Esc"
  39.         Command="{Binding BackCommand}"
  40.         Modifiers="" />
  41. </Window.InputBindings>{
  42. <Window.InputBindings>
  43.     <KeyBinding
  44.         Key="Esc"
  45.         Command="{Binding BackCommand}"
  46.         Modifiers="" />
  47. </Window.InputBindings><Window.InputBindings>
  48.     <KeyBinding
  49.         Key="Esc"
  50.         Command="{Binding BackCommand}"
  51.         Modifiers="" />
  52. </Window.InputBindings>//如果全局禁用,则不跟踪脏数据状态
  53. <Window.InputBindings>
  54.     <KeyBinding
  55.         Key="Esc"
  56.         Command="{Binding BackCommand}"
  57.         Modifiers="" />
  58. </Window.InputBindings><Window.InputBindings>
  59.     <KeyBinding
  60.         Key="Esc"
  61.         Command="{Binding BackCommand}"
  62.         Modifiers="" />
  63. </Window.InputBindings>if (App.ViewModel!.DisableDirtyMessage)
  64. <Window.InputBindings>
  65.     <KeyBinding
  66.         Key="Esc"
  67.         Command="{Binding BackCommand}"
  68.         Modifiers="" />
  69. </Window.InputBindings><Window.InputBindings>
  70.     <KeyBinding
  71.         Key="Esc"
  72.         Command="{Binding BackCommand}"
  73.         Modifiers="" />
  74. </Window.InputBindings><Window.InputBindings>
  75.     <KeyBinding
  76.         Key="Esc"
  77.         Command="{Binding BackCommand}"
  78.         Modifiers="" />
  79. </Window.InputBindings>return;
  80. <Window.InputBindings>
  81.     <KeyBinding
  82.         Key="Esc"
  83.         Command="{Binding BackCommand}"
  84.         Modifiers="" />
  85. </Window.InputBindings><Window.InputBindings>
  86.     <KeyBinding
  87.         Key="Esc"
  88.         Command="{Binding BackCommand}"
  89.         Modifiers="" />
  90. </Window.InputBindings>#region 对控件类型进行监控
  91. <Window.InputBindings>
  92.     <KeyBinding
  93.         Key="Esc"
  94.         Command="{Binding BackCommand}"
  95.         Modifiers="" />
  96. </Window.InputBindings><Window.InputBindings>
  97.     <KeyBinding
  98.         Key="Esc"
  99.         Command="{Binding BackCommand}"
  100.         Modifiers="" />
  101. </Window.InputBindings>//文本控件
  102. <Window.InputBindings>
  103.     <KeyBinding
  104.         Key="Esc"
  105.         Command="{Binding BackCommand}"
  106.         Modifiers="" />
  107. </Window.InputBindings><Window.InputBindings>
  108.     <KeyBinding
  109.         Key="Esc"
  110.         Command="{Binding BackCommand}"
  111.         Modifiers="" />
  112. </Window.InputBindings>var texboxList = FindLogicalChildren<TextBoxBase>(depObj);
  113. <Window.InputBindings>
  114.     <KeyBinding
  115.         Key="Esc"
  116.         Command="{Binding BackCommand}"
  117.         Modifiers="" />
  118. </Window.InputBindings><Window.InputBindings>
  119.     <KeyBinding
  120.         Key="Esc"
  121.         Command="{Binding BackCommand}"
  122.         Modifiers="" />
  123. </Window.InputBindings>foreach (var textbox in texboxList)
  124. <Window.InputBindings>
  125.     <KeyBinding
  126.         Key="Esc"
  127.         Command="{Binding BackCommand}"
  128.         Modifiers="" />
  129. </Window.InputBindings><Window.InputBindings>
  130.     <KeyBinding
  131.         Key="Esc"
  132.         Command="{Binding BackCommand}"
  133.         Modifiers="" />
  134. </Window.InputBindings>{
  135. <Window.InputBindings>
  136.     <KeyBinding
  137.         Key="Esc"
  138.         Command="{Binding BackCommand}"
  139.         Modifiers="" />
  140. </Window.InputBindings><Window.InputBindings>
  141.     <KeyBinding
  142.         Key="Esc"
  143.         Command="{Binding BackCommand}"
  144.         Modifiers="" />
  145. </Window.InputBindings><Window.InputBindings>
  146.     <KeyBinding
  147.         Key="Esc"
  148.         Command="{Binding BackCommand}"
  149.         Modifiers="" />
  150. </Window.InputBindings>if (textbox != null && !textbox.IsReadOnly)
  151. <Window.InputBindings>
  152.     <KeyBinding
  153.         Key="Esc"
  154.         Command="{Binding BackCommand}"
  155.         Modifiers="" />
  156. </Window.InputBindings><Window.InputBindings>
  157.     <KeyBinding
  158.         Key="Esc"
  159.         Command="{Binding BackCommand}"
  160.         Modifiers="" />
  161. </Window.InputBindings><Window.InputBindings>
  162.     <KeyBinding
  163.         Key="Esc"
  164.         Command="{Binding BackCommand}"
  165.         Modifiers="" />
  166. </Window.InputBindings>{
  167. <Window.InputBindings>
  168.     <KeyBinding
  169.         Key="Esc"
  170.         Command="{Binding BackCommand}"
  171.         Modifiers="" />
  172. </Window.InputBindings><Window.InputBindings>
  173.     <KeyBinding
  174.         Key="Esc"
  175.         Command="{Binding BackCommand}"
  176.         Modifiers="" />
  177. </Window.InputBindings><Window.InputBindings>
  178.     <KeyBinding
  179.         Key="Esc"
  180.         Command="{Binding BackCommand}"
  181.         Modifiers="" />
  182. </Window.InputBindings><Window.InputBindings>
  183.     <KeyBinding
  184.         Key="Esc"
  185.         Command="{Binding BackCommand}"
  186.         Modifiers="" />
  187. </Window.InputBindings>textbox.TextChanged += (s, e) => { MainModelHelper.SetIsDirty(true); };//文本变化触发
  188. <Window.InputBindings>
  189.     <KeyBinding
  190.         Key="Esc"
  191.         Command="{Binding BackCommand}"
  192.         Modifiers="" />
  193. </Window.InputBindings><Window.InputBindings>
  194.     <KeyBinding
  195.         Key="Esc"
  196.         Command="{Binding BackCommand}"
  197.         Modifiers="" />
  198. </Window.InputBindings><Window.InputBindings>
  199.     <KeyBinding
  200.         Key="Esc"
  201.         Command="{Binding BackCommand}"
  202.         Modifiers="" />
  203. </Window.InputBindings>}
  204. <Window.InputBindings>
  205.     <KeyBinding
  206.         Key="Esc"
  207.         Command="{Binding BackCommand}"
  208.         Modifiers="" />
  209. </Window.InputBindings><Window.InputBindings>
  210.     <KeyBinding
  211.         Key="Esc"
  212.         Command="{Binding BackCommand}"
  213.         Modifiers="" />
  214. </Window.InputBindings>}
  215. <Window.InputBindings>
  216.     <KeyBinding
  217.         Key="Esc"
  218.         Command="{Binding BackCommand}"
  219.         Modifiers="" />
  220. </Window.InputBindings><Window.InputBindings>
  221.     <KeyBinding
  222.         Key="Esc"
  223.         Command="{Binding BackCommand}"
  224.         Modifiers="" />
  225. </Window.InputBindings>//下拉列表
  226. <Window.InputBindings>
  227.     <KeyBinding
  228.         Key="Esc"
  229.         Command="{Binding BackCommand}"
  230.         Modifiers="" />
  231. </Window.InputBindings><Window.InputBindings>
  232.     <KeyBinding
  233.         Key="Esc"
  234.         Command="{Binding BackCommand}"
  235.         Modifiers="" />
  236. </Window.InputBindings>var selectorList = FindLogicalChildren<Selector>(depObj);
  237. <Window.InputBindings>
  238.     <KeyBinding
  239.         Key="Esc"
  240.         Command="{Binding BackCommand}"
  241.         Modifiers="" />
  242. </Window.InputBindings><Window.InputBindings>
  243.     <KeyBinding
  244.         Key="Esc"
  245.         Command="{Binding BackCommand}"
  246.         Modifiers="" />
  247. </Window.InputBindings>foreach (var selector in selectorList)
  248. <Window.InputBindings>
  249.     <KeyBinding
  250.         Key="Esc"
  251.         Command="{Binding BackCommand}"
  252.         Modifiers="" />
  253. </Window.InputBindings><Window.InputBindings>
  254.     <KeyBinding
  255.         Key="Esc"
  256.         Command="{Binding BackCommand}"
  257.         Modifiers="" />
  258. </Window.InputBindings>{
  259. <Window.InputBindings>
  260.     <KeyBinding
  261.         Key="Esc"
  262.         Command="{Binding BackCommand}"
  263.         Modifiers="" />
  264. </Window.InputBindings><Window.InputBindings>
  265.     <KeyBinding
  266.         Key="Esc"
  267.         Command="{Binding BackCommand}"
  268.         Modifiers="" />
  269. </Window.InputBindings><Window.InputBindings>
  270.     <KeyBinding
  271.         Key="Esc"
  272.         Command="{Binding BackCommand}"
  273.         Modifiers="" />
  274. </Window.InputBindings>var selectorType = selector.GetType();
  275. <Window.InputBindings>
  276.     <KeyBinding
  277.         Key="Esc"
  278.         Command="{Binding BackCommand}"
  279.         Modifiers="" />
  280. </Window.InputBindings><Window.InputBindings>
  281.     <KeyBinding
  282.         Key="Esc"
  283.         Command="{Binding BackCommand}"
  284.         Modifiers="" />
  285. </Window.InputBindings><Window.InputBindings>
  286.     <KeyBinding
  287.         Key="Esc"
  288.         Command="{Binding BackCommand}"
  289.         Modifiers="" />
  290. </Window.InputBindings>if (selector != null && selector.IsEnabled &&
  291. <Window.InputBindings>
  292.     <KeyBinding
  293.         Key="Esc"
  294.         Command="{Binding BackCommand}"
  295.         Modifiers="" />
  296. </Window.InputBindings><Window.InputBindings>
  297.     <KeyBinding
  298.         Key="Esc"
  299.         Command="{Binding BackCommand}"
  300.         Modifiers="" />
  301. </Window.InputBindings><Window.InputBindings>
  302.     <KeyBinding
  303.         Key="Esc"
  304.         Command="{Binding BackCommand}"
  305.         Modifiers="" />
  306. </Window.InputBindings>   selectorType != typeof(TabControl) && selectorType != typeof(ListBox)) //排除TabControl和ListBox选择触发
  307. <Window.InputBindings>
  308.     <KeyBinding
  309.         Key="Esc"
  310.         Command="{Binding BackCommand}"
  311.         Modifiers="" />
  312. </Window.InputBindings><Window.InputBindings>
  313.     <KeyBinding
  314.         Key="Esc"
  315.         Command="{Binding BackCommand}"
  316.         Modifiers="" />
  317. </Window.InputBindings><Window.InputBindings>
  318.     <KeyBinding
  319.         Key="Esc"
  320.         Command="{Binding BackCommand}"
  321.         Modifiers="" />
  322. </Window.InputBindings>{
  323. <Window.InputBindings>
  324.     <KeyBinding
  325.         Key="Esc"
  326.         Command="{Binding BackCommand}"
  327.         Modifiers="" />
  328. </Window.InputBindings><Window.InputBindings>
  329.     <KeyBinding
  330.         Key="Esc"
  331.         Command="{Binding BackCommand}"
  332.         Modifiers="" />
  333. </Window.InputBindings><Window.InputBindings>
  334.     <KeyBinding
  335.         Key="Esc"
  336.         Command="{Binding BackCommand}"
  337.         Modifiers="" />
  338. </Window.InputBindings><Window.InputBindings>
  339.     <KeyBinding
  340.         Key="Esc"
  341.         Command="{Binding BackCommand}"
  342.         Modifiers="" />
  343. </Window.InputBindings>selector.SelectionChanged += (s, e) => { MainModelHelper.SetIsDirty(true); };//选择变化触发
  344. <Window.InputBindings>
  345.     <KeyBinding
  346.         Key="Esc"
  347.         Command="{Binding BackCommand}"
  348.         Modifiers="" />
  349. </Window.InputBindings><Window.InputBindings>
  350.     <KeyBinding
  351.         Key="Esc"
  352.         Command="{Binding BackCommand}"
  353.         Modifiers="" />
  354. </Window.InputBindings><Window.InputBindings>
  355.     <KeyBinding
  356.         Key="Esc"
  357.         Command="{Binding BackCommand}"
  358.         Modifiers="" />
  359. </Window.InputBindings>}
  360. <Window.InputBindings>
  361.     <KeyBinding
  362.         Key="Esc"
  363.         Command="{Binding BackCommand}"
  364.         Modifiers="" />
  365. </Window.InputBindings><Window.InputBindings>
  366.     <KeyBinding
  367.         Key="Esc"
  368.         Command="{Binding BackCommand}"
  369.         Modifiers="" />
  370. </Window.InputBindings>}
  371. <Window.InputBindings>
  372.     <KeyBinding
  373.         Key="Esc"
  374.         Command="{Binding BackCommand}"
  375.         Modifiers="" />
  376. </Window.InputBindings><Window.InputBindings>
  377.     <KeyBinding
  378.         Key="Esc"
  379.         Command="{Binding BackCommand}"
  380.         Modifiers="" />
  381. </Window.InputBindings>//ToggleButton,包含CheckBox、RadioButton
  382. <Window.InputBindings>
  383.     <KeyBinding
  384.         Key="Esc"
  385.         Command="{Binding BackCommand}"
  386.         Modifiers="" />
  387. </Window.InputBindings><Window.InputBindings>
  388.     <KeyBinding
  389.         Key="Esc"
  390.         Command="{Binding BackCommand}"
  391.         Modifiers="" />
  392. </Window.InputBindings>var buttonList = FindLogicalChildren<ToggleButton>(depObj);
  393. <Window.InputBindings>
  394.     <KeyBinding
  395.         Key="Esc"
  396.         Command="{Binding BackCommand}"
  397.         Modifiers="" />
  398. </Window.InputBindings><Window.InputBindings>
  399.     <KeyBinding
  400.         Key="Esc"
  401.         Command="{Binding BackCommand}"
  402.         Modifiers="" />
  403. </Window.InputBindings>foreach (var toggle in buttonList)
  404. <Window.InputBindings>
  405.     <KeyBinding
  406.         Key="Esc"
  407.         Command="{Binding BackCommand}"
  408.         Modifiers="" />
  409. </Window.InputBindings><Window.InputBindings>
  410.     <KeyBinding
  411.         Key="Esc"
  412.         Command="{Binding BackCommand}"
  413.         Modifiers="" />
  414. </Window.InputBindings>{
  415. <Window.InputBindings>
  416.     <KeyBinding
  417.         Key="Esc"
  418.         Command="{Binding BackCommand}"
  419.         Modifiers="" />
  420. </Window.InputBindings><Window.InputBindings>
  421.     <KeyBinding
  422.         Key="Esc"
  423.         Command="{Binding BackCommand}"
  424.         Modifiers="" />
  425. </Window.InputBindings><Window.InputBindings>
  426.     <KeyBinding
  427.         Key="Esc"
  428.         Command="{Binding BackCommand}"
  429.         Modifiers="" />
  430. </Window.InputBindings>if (toggle != null && toggle.IsEnabled)
  431. <Window.InputBindings>
  432.     <KeyBinding
  433.         Key="Esc"
  434.         Command="{Binding BackCommand}"
  435.         Modifiers="" />
  436. </Window.InputBindings><Window.InputBindings>
  437.     <KeyBinding
  438.         Key="Esc"
  439.         Command="{Binding BackCommand}"
  440.         Modifiers="" />
  441. </Window.InputBindings><Window.InputBindings>
  442.     <KeyBinding
  443.         Key="Esc"
  444.         Command="{Binding BackCommand}"
  445.         Modifiers="" />
  446. </Window.InputBindings>{
  447. <Window.InputBindings>
  448.     <KeyBinding
  449.         Key="Esc"
  450.         Command="{Binding BackCommand}"
  451.         Modifiers="" />
  452. </Window.InputBindings><Window.InputBindings>
  453.     <KeyBinding
  454.         Key="Esc"
  455.         Command="{Binding BackCommand}"
  456.         Modifiers="" />
  457. </Window.InputBindings><Window.InputBindings>
  458.     <KeyBinding
  459.         Key="Esc"
  460.         Command="{Binding BackCommand}"
  461.         Modifiers="" />
  462. </Window.InputBindings><Window.InputBindings>
  463.     <KeyBinding
  464.         Key="Esc"
  465.         Command="{Binding BackCommand}"
  466.         Modifiers="" />
  467. </Window.InputBindings>toggle.Checked += (s, e) => { MainModelHelper.SetIsDirty(true); };//选择变化触发
  468. <Window.InputBindings>
  469.     <KeyBinding
  470.         Key="Esc"
  471.         Command="{Binding BackCommand}"
  472.         Modifiers="" />
  473. </Window.InputBindings><Window.InputBindings>
  474.     <KeyBinding
  475.         Key="Esc"
  476.         Command="{Binding BackCommand}"
  477.         Modifiers="" />
  478. </Window.InputBindings><Window.InputBindings>
  479.     <KeyBinding
  480.         Key="Esc"
  481.         Command="{Binding BackCommand}"
  482.         Modifiers="" />
  483. </Window.InputBindings><Window.InputBindings>
  484.     <KeyBinding
  485.         Key="Esc"
  486.         Command="{Binding BackCommand}"
  487.         Modifiers="" />
  488. </Window.InputBindings>toggle.Unchecked += (s, e) => { MainModelHelper.SetIsDirty(true); };//选择变化触发
  489. <Window.InputBindings>
  490.     <KeyBinding
  491.         Key="Esc"
  492.         Command="{Binding BackCommand}"
  493.         Modifiers="" />
  494. </Window.InputBindings><Window.InputBindings>
  495.     <KeyBinding
  496.         Key="Esc"
  497.         Command="{Binding BackCommand}"
  498.         Modifiers="" />
  499. </Window.InputBindings><Window.InputBindings>
  500.     <KeyBinding
  501.         Key="Esc"
  502.         Command="{Binding BackCommand}"
  503.         Modifiers="" />
  504. </Window.InputBindings>}
  505. <Window.InputBindings>
  506.     <KeyBinding
  507.         Key="Esc"
  508.         Command="{Binding BackCommand}"
  509.         Modifiers="" />
  510. </Window.InputBindings><Window.InputBindings>
  511.     <KeyBinding
  512.         Key="Esc"
  513.         Command="{Binding BackCommand}"
  514.         Modifiers="" />
  515. </Window.InputBindings>}
  516. <Window.InputBindings>
  517.     <KeyBinding
  518.         Key="Esc"
  519.         Command="{Binding BackCommand}"
  520.         Modifiers="" />
  521. </Window.InputBindings><Window.InputBindings>
  522.     <KeyBinding
  523.         Key="Esc"
  524.         Command="{Binding BackCommand}"
  525.         Modifiers="" />
  526. </Window.InputBindings>#endregion
  527. <Window.InputBindings>
  528.     <KeyBinding
  529.         Key="Esc"
  530.         Command="{Binding BackCommand}"
  531.         Modifiers="" />
  532. </Window.InputBindings>}
复制代码
因此,对于窗口的控件的编辑状态跟踪,我们可以在窗口的Loaded或者ContentRendered中实现跟踪即可,我这里实现覆盖OnContentRendered的方式,来对窗口控件的统一跟踪。
  1. <Window.InputBindings>
  2.     <KeyBinding
  3.         Key="Esc"
  4.         Command="{Binding BackCommand}"
  5.         Modifiers="" />
  6. </Window.InputBindings>/// <summary>
  7. <Window.InputBindings>
  8.     <KeyBinding
  9.         Key="Esc"
  10.         Command="{Binding BackCommand}"
  11.         Modifiers="" />
  12. </Window.InputBindings>/// 该事件在loaded之后执行,也是在所有元素渲染结束之后执行
  13. <Window.InputBindings>
  14.     <KeyBinding
  15.         Key="Esc"
  16.         Command="{Binding BackCommand}"
  17.         Modifiers="" />
  18. </Window.InputBindings>/// </summary>
  19. <Window.InputBindings>
  20.     <KeyBinding
  21.         Key="Esc"
  22.         Command="{Binding BackCommand}"
  23.         Modifiers="" />
  24. </Window.InputBindings>protected override void OnContentRendered(EventArgs e)
  25. <Window.InputBindings>
  26.     <KeyBinding
  27.         Key="Esc"
  28.         Command="{Binding BackCommand}"
  29.         Modifiers="" />
  30. </Window.InputBindings>{
  31. <Window.InputBindings>
  32.     <KeyBinding
  33.         Key="Esc"
  34.         Command="{Binding BackCommand}"
  35.         Modifiers="" />
  36. </Window.InputBindings><Window.InputBindings>
  37.     <KeyBinding
  38.         Key="Esc"
  39.         Command="{Binding BackCommand}"
  40.         Modifiers="" />
  41. </Window.InputBindings>base.OnContentRendered(e);//初始化后默认脏数据状态为false
  42. <Window.InputBindings>
  43.     <KeyBinding
  44.         Key="Esc"
  45.         Command="{Binding BackCommand}"
  46.         Modifiers="" />
  47. </Window.InputBindings><Window.InputBindings>
  48.     <KeyBinding
  49.         Key="Esc"
  50.         Command="{Binding BackCommand}"
  51.         Modifiers="" />
  52. </Window.InputBindings>App.ViewModel!.IsDirty = false;
  53. <Window.InputBindings>
  54.     <KeyBinding
  55.         Key="Esc"
  56.         Command="{Binding BackCommand}"
  57.         Modifiers="" />
  58. </Window.InputBindings><Window.InputBindings>
  59.     <KeyBinding
  60.         Key="Esc"
  61.         Command="{Binding BackCommand}"
  62.         Modifiers="" />
  63. </Window.InputBindings>//对控件变化进行跟踪, 遍历父级及子级节点
  64. <Window.InputBindings>
  65.     <KeyBinding
  66.         Key="Esc"
  67.         Command="{Binding BackCommand}"
  68.         Modifiers="" />
  69. </Window.InputBindings><Window.InputBindings>
  70.     <KeyBinding
  71.         Key="Esc"
  72.         Command="{Binding BackCommand}"
  73.         Modifiers="" />
  74. </Window.InputBindings>ControlHelper.SetDirtyEvent(this);
  75. <Window.InputBindings>
  76.     <KeyBinding
  77.         Key="Esc"
  78.         Command="{Binding BackCommand}"
  79.         Modifiers="" />
  80. </Window.InputBindings><Window.InputBindings>
  81.     <KeyBinding
  82.         Key="Esc"
  83.         Command="{Binding BackCommand}"
  84.         Modifiers="" />
  85. </Window.InputBindings>//如果修改内容,对退出窗口进行确认
  86. <Window.InputBindings>
  87.     <KeyBinding
  88.         Key="Esc"
  89.         Command="{Binding BackCommand}"
  90.         Modifiers="" />
  91. </Window.InputBindings><Window.InputBindings>
  92.     <KeyBinding
  93.         Key="Esc"
  94.         Command="{Binding BackCommand}"
  95.         Modifiers="" />
  96. </Window.InputBindings>this.Closing += (s, e) =>
  97. <Window.InputBindings>
  98.     <KeyBinding
  99.         Key="Esc"
  100.         Command="{Binding BackCommand}"
  101.         Modifiers="" />
  102. </Window.InputBindings><Window.InputBindings>
  103.     <KeyBinding
  104.         Key="Esc"
  105.         Command="{Binding BackCommand}"
  106.         Modifiers="" />
  107. </Window.InputBindings>{
  108. <Window.InputBindings>
  109.     <KeyBinding
  110.         Key="Esc"
  111.         Command="{Binding BackCommand}"
  112.         Modifiers="" />
  113. </Window.InputBindings><Window.InputBindings>
  114.     <KeyBinding
  115.         Key="Esc"
  116.         Command="{Binding BackCommand}"
  117.         Modifiers="" />
  118. </Window.InputBindings><Window.InputBindings>
  119.     <KeyBinding
  120.         Key="Esc"
  121.         Command="{Binding BackCommand}"
  122.         Modifiers="" />
  123. </Window.InputBindings>var isDisableDirty = App.ViewModel!.DisableDirtyMessage; //是否禁用了脏数据提示
  124. <Window.InputBindings>
  125.     <KeyBinding
  126.         Key="Esc"
  127.         Command="{Binding BackCommand}"
  128.         Modifiers="" />
  129. </Window.InputBindings><Window.InputBindings>
  130.     <KeyBinding
  131.         Key="Esc"
  132.         Command="{Binding BackCommand}"
  133.         Modifiers="" />
  134. </Window.InputBindings><Window.InputBindings>
  135.     <KeyBinding
  136.         Key="Esc"
  137.         Command="{Binding BackCommand}"
  138.         Modifiers="" />
  139. </Window.InputBindings>var isDirty = App.ViewModel!.IsDirty;
  140. <Window.InputBindings>
  141.     <KeyBinding
  142.         Key="Esc"
  143.         Command="{Binding BackCommand}"
  144.         Modifiers="" />
  145. </Window.InputBindings><Window.InputBindings>
  146.     <KeyBinding
  147.         Key="Esc"
  148.         Command="{Binding BackCommand}"
  149.         Modifiers="" />
  150. </Window.InputBindings><Window.InputBindings>
  151.     <KeyBinding
  152.         Key="Esc"
  153.         Command="{Binding BackCommand}"
  154.         Modifiers="" />
  155. </Window.InputBindings>if (!isDisableDirty && isDirty)
  156. <Window.InputBindings>
  157.     <KeyBinding
  158.         Key="Esc"
  159.         Command="{Binding BackCommand}"
  160.         Modifiers="" />
  161. </Window.InputBindings><Window.InputBindings>
  162.     <KeyBinding
  163.         Key="Esc"
  164.         Command="{Binding BackCommand}"
  165.         Modifiers="" />
  166. </Window.InputBindings><Window.InputBindings>
  167.     <KeyBinding
  168.         Key="Esc"
  169.         Command="{Binding BackCommand}"
  170.         Modifiers="" />
  171. </Window.InputBindings>{
  172. <Window.InputBindings>
  173.     <KeyBinding
  174.         Key="Esc"
  175.         Command="{Binding BackCommand}"
  176.         Modifiers="" />
  177. </Window.InputBindings><Window.InputBindings>
  178.     <KeyBinding
  179.         Key="Esc"
  180.         Command="{Binding BackCommand}"
  181.         Modifiers="" />
  182. </Window.InputBindings><Window.InputBindings>
  183.     <KeyBinding
  184.         Key="Esc"
  185.         Command="{Binding BackCommand}"
  186.         Modifiers="" />
  187. </Window.InputBindings><Window.InputBindings>
  188.     <KeyBinding
  189.         Key="Esc"
  190.         Command="{Binding BackCommand}"
  191.         Modifiers="" />
  192. </Window.InputBindings>//数据已脏,提示确认
  193. <Window.InputBindings>
  194.     <KeyBinding
  195.         Key="Esc"
  196.         Command="{Binding BackCommand}"
  197.         Modifiers="" />
  198. </Window.InputBindings><Window.InputBindings>
  199.     <KeyBinding
  200.         Key="Esc"
  201.         Command="{Binding BackCommand}"
  202.         Modifiers="" />
  203. </Window.InputBindings><Window.InputBindings>
  204.     <KeyBinding
  205.         Key="Esc"
  206.         Command="{Binding BackCommand}"
  207.         Modifiers="" />
  208. </Window.InputBindings><Window.InputBindings>
  209.     <KeyBinding
  210.         Key="Esc"
  211.         Command="{Binding BackCommand}"
  212.         Modifiers="" />
  213. </Window.InputBindings>if (MessageDxUtil.ShowYesNoAndWarning("界面控件数据已编辑过,是否确认丢弃并关闭窗口") != System.Windows.MessageBoxResult.Yes)
  214. <Window.InputBindings>
  215.     <KeyBinding
  216.         Key="Esc"
  217.         Command="{Binding BackCommand}"
  218.         Modifiers="" />
  219. </Window.InputBindings><Window.InputBindings>
  220.     <KeyBinding
  221.         Key="Esc"
  222.         Command="{Binding BackCommand}"
  223.         Modifiers="" />
  224. </Window.InputBindings><Window.InputBindings>
  225.     <KeyBinding
  226.         Key="Esc"
  227.         Command="{Binding BackCommand}"
  228.         Modifiers="" />
  229. </Window.InputBindings><Window.InputBindings>
  230.     <KeyBinding
  231.         Key="Esc"
  232.         Command="{Binding BackCommand}"
  233.         Modifiers="" />
  234. </Window.InputBindings>{
  235. <Window.InputBindings>
  236.     <KeyBinding
  237.         Key="Esc"
  238.         Command="{Binding BackCommand}"
  239.         Modifiers="" />
  240. </Window.InputBindings><Window.InputBindings>
  241.     <KeyBinding
  242.         Key="Esc"
  243.         Command="{Binding BackCommand}"
  244.         Modifiers="" />
  245. </Window.InputBindings><Window.InputBindings>
  246.     <KeyBinding
  247.         Key="Esc"
  248.         Command="{Binding BackCommand}"
  249.         Modifiers="" />
  250. </Window.InputBindings><Window.InputBindings>
  251.     <KeyBinding
  252.         Key="Esc"
  253.         Command="{Binding BackCommand}"
  254.         Modifiers="" />
  255. </Window.InputBindings><Window.InputBindings>
  256.     <KeyBinding
  257.         Key="Esc"
  258.         Command="{Binding BackCommand}"
  259.         Modifiers="" />
  260. </Window.InputBindings>e.Cancel = true;
  261. <Window.InputBindings>
  262.     <KeyBinding
  263.         Key="Esc"
  264.         Command="{Binding BackCommand}"
  265.         Modifiers="" />
  266. </Window.InputBindings><Window.InputBindings>
  267.     <KeyBinding
  268.         Key="Esc"
  269.         Command="{Binding BackCommand}"
  270.         Modifiers="" />
  271. </Window.InputBindings><Window.InputBindings>
  272.     <KeyBinding
  273.         Key="Esc"
  274.         Command="{Binding BackCommand}"
  275.         Modifiers="" />
  276. </Window.InputBindings><Window.InputBindings>
  277.     <KeyBinding
  278.         Key="Esc"
  279.         Command="{Binding BackCommand}"
  280.         Modifiers="" />
  281. </Window.InputBindings>}
  282. <Window.InputBindings>
  283.     <KeyBinding
  284.         Key="Esc"
  285.         Command="{Binding BackCommand}"
  286.         Modifiers="" />
  287. </Window.InputBindings><Window.InputBindings>
  288.     <KeyBinding
  289.         Key="Esc"
  290.         Command="{Binding BackCommand}"
  291.         Modifiers="" />
  292. </Window.InputBindings><Window.InputBindings>
  293.     <KeyBinding
  294.         Key="Esc"
  295.         Command="{Binding BackCommand}"
  296.         Modifiers="" />
  297. </Window.InputBindings><Window.InputBindings>
  298.     <KeyBinding
  299.         Key="Esc"
  300.         Command="{Binding BackCommand}"
  301.         Modifiers="" />
  302. </Window.InputBindings>else
  303. <Window.InputBindings>
  304.     <KeyBinding
  305.         Key="Esc"
  306.         Command="{Binding BackCommand}"
  307.         Modifiers="" />
  308. </Window.InputBindings><Window.InputBindings>
  309.     <KeyBinding
  310.         Key="Esc"
  311.         Command="{Binding BackCommand}"
  312.         Modifiers="" />
  313. </Window.InputBindings><Window.InputBindings>
  314.     <KeyBinding
  315.         Key="Esc"
  316.         Command="{Binding BackCommand}"
  317.         Modifiers="" />
  318. </Window.InputBindings><Window.InputBindings>
  319.     <KeyBinding
  320.         Key="Esc"
  321.         Command="{Binding BackCommand}"
  322.         Modifiers="" />
  323. </Window.InputBindings>{
  324. <Window.InputBindings>
  325.     <KeyBinding
  326.         Key="Esc"
  327.         Command="{Binding BackCommand}"
  328.         Modifiers="" />
  329. </Window.InputBindings><Window.InputBindings>
  330.     <KeyBinding
  331.         Key="Esc"
  332.         Command="{Binding BackCommand}"
  333.         Modifiers="" />
  334. </Window.InputBindings><Window.InputBindings>
  335.     <KeyBinding
  336.         Key="Esc"
  337.         Command="{Binding BackCommand}"
  338.         Modifiers="" />
  339. </Window.InputBindings><Window.InputBindings>
  340.     <KeyBinding
  341.         Key="Esc"
  342.         Command="{Binding BackCommand}"
  343.         Modifiers="" />
  344. </Window.InputBindings><Window.InputBindings>
  345.     <KeyBinding
  346.         Key="Esc"
  347.         Command="{Binding BackCommand}"
  348.         Modifiers="" />
  349. </Window.InputBindings>App.ViewModel!.IsDirty = false;//取消脏数据状态
  350. <Window.InputBindings>
  351.     <KeyBinding
  352.         Key="Esc"
  353.         Command="{Binding BackCommand}"
  354.         Modifiers="" />
  355. </Window.InputBindings><Window.InputBindings>
  356.     <KeyBinding
  357.         Key="Esc"
  358.         Command="{Binding BackCommand}"
  359.         Modifiers="" />
  360. </Window.InputBindings><Window.InputBindings>
  361.     <KeyBinding
  362.         Key="Esc"
  363.         Command="{Binding BackCommand}"
  364.         Modifiers="" />
  365. </Window.InputBindings><Window.InputBindings>
  366.     <KeyBinding
  367.         Key="Esc"
  368.         Command="{Binding BackCommand}"
  369.         Modifiers="" />
  370. </Window.InputBindings><Window.InputBindings>
  371.     <KeyBinding
  372.         Key="Esc"
  373.         Command="{Binding BackCommand}"
  374.         Modifiers="" />
  375. </Window.InputBindings>GrowlUtil.ClearTips(); //清空提示
  376. <Window.InputBindings>
  377.     <KeyBinding
  378.         Key="Esc"
  379.         Command="{Binding BackCommand}"
  380.         Modifiers="" />
  381. </Window.InputBindings><Window.InputBindings>
  382.     <KeyBinding
  383.         Key="Esc"
  384.         Command="{Binding BackCommand}"
  385.         Modifiers="" />
  386. </Window.InputBindings><Window.InputBindings>
  387.     <KeyBinding
  388.         Key="Esc"
  389.         Command="{Binding BackCommand}"
  390.         Modifiers="" />
  391. </Window.InputBindings><Window.InputBindings>
  392.     <KeyBinding
  393.         Key="Esc"
  394.         Command="{Binding BackCommand}"
  395.         Modifiers="" />
  396. </Window.InputBindings>}
  397. <Window.InputBindings>
  398.     <KeyBinding
  399.         Key="Esc"
  400.         Command="{Binding BackCommand}"
  401.         Modifiers="" />
  402. </Window.InputBindings><Window.InputBindings>
  403.     <KeyBinding
  404.         Key="Esc"
  405.         Command="{Binding BackCommand}"
  406.         Modifiers="" />
  407. </Window.InputBindings><Window.InputBindings>
  408.     <KeyBinding
  409.         Key="Esc"
  410.         Command="{Binding BackCommand}"
  411.         Modifiers="" />
  412. </Window.InputBindings>}
  413. <Window.InputBindings>
  414.     <KeyBinding
  415.         Key="Esc"
  416.         Command="{Binding BackCommand}"
  417.         Modifiers="" />
  418. </Window.InputBindings><Window.InputBindings>
  419.     <KeyBinding
  420.         Key="Esc"
  421.         Command="{Binding BackCommand}"
  422.         Modifiers="" />
  423. </Window.InputBindings>};
  424. <Window.InputBindings>
  425.     <KeyBinding
  426.         Key="Esc"
  427.         Command="{Binding BackCommand}"
  428.         Modifiers="" />
  429. </Window.InputBindings>}
复制代码
但是每个编辑窗口这样做,肯定是代码冗余的,我们优化一下,把逻辑抽取到一个独立的辅助类里面处理,这里改进后代码如下所示。
  1. /// <summary>
  2. /// 该事件在loaded之后执行,也是在所有元素渲染结束之后执行
  3. /// </summary>
  4. protected override void OnContentRendered(EventArgs e)
  5. {
  6. <Window.InputBindings>
  7.     <KeyBinding
  8.         Key="Esc"
  9.         Command="{Binding BackCommand}"
  10.         Modifiers="" />
  11. </Window.InputBindings>base.OnContentRendered(e);
  12. <Window.InputBindings>
  13.     <KeyBinding
  14.         Key="Esc"
  15.         Command="{Binding BackCommand}"
  16.         Modifiers="" />
  17. </Window.InputBindings>//在窗口准备完成后,对控件的内容变化进行监控,如修改过则退出确认
  18. <Window.InputBindings>
  19.     <KeyBinding
  20.         Key="Esc"
  21.         Command="{Binding BackCommand}"
  22.         Modifiers="" />
  23. </Window.InputBindings>ControlHelper.SetDirtyWindow(this);
  24. }
复制代码
这样在页面关闭的时候,我们提示用户即可。

当然,我们在主窗口视图模型里面也是设置了一个总开关,不需要的时候,关闭它即可。
  1. <strong>App.ViewModel!.DisableDirtyMessage</strong>
复制代码
 我们在上面的代码逻辑中也可以看到,我们如果确认丢弃修改内容,那么状态重置,并清空一些提示信息即可。
  1. <strong>App.ViewModel!.IsDirty</strong> = false;//取消脏数据状态
  2. GrowlUtil.ClearTips(); //清空提示
复制代码
我们前面说到窗口对象的注入都是以Transient的方式,窗口的打开都是以每次构建新对象的方式,视图模型则是共享方式,因此,我们打开窗口的操作如下所示。
  1. /// <summary>
  2. /// 批量添加页面处理
  3. /// </summary>
  4. [RelayCommand]
  5. private void BatchAdd()
  6. {
  7. <Window.InputBindings>
  8.     <KeyBinding
  9.         Key="Esc"
  10.         Command="{Binding BackCommand}"
  11.         Modifiers="" />
  12. </Window.InputBindings>if (this.ViewModel.SelectDictType == null)
  13. <Window.InputBindings>
  14.     <KeyBinding
  15.         Key="Esc"
  16.         Command="{Binding BackCommand}"
  17.         Modifiers="" />
  18. </Window.InputBindings>{
  19. <Window.InputBindings>
  20.     <KeyBinding
  21.         Key="Esc"
  22.         Command="{Binding BackCommand}"
  23.         Modifiers="" />
  24. </Window.InputBindings><Window.InputBindings>
  25.     <KeyBinding
  26.         Key="Esc"
  27.         Command="{Binding BackCommand}"
  28.         Modifiers="" />
  29. </Window.InputBindings>GrowlUtil.ShowInfo("请选择大类再添加项目");
  30. <Window.InputBindings>
  31.     <KeyBinding
  32.         Key="Esc"
  33.         Command="{Binding BackCommand}"
  34.         Modifiers="" />
  35. </Window.InputBindings><Window.InputBindings>
  36.     <KeyBinding
  37.         Key="Esc"
  38.         Command="{Binding BackCommand}"
  39.         Modifiers="" />
  40. </Window.InputBindings>return;
  41. <Window.InputBindings>
  42.     <KeyBinding
  43.         Key="Esc"
  44.         Command="{Binding BackCommand}"
  45.         Modifiers="" />
  46. </Window.InputBindings>}
  47. <Window.InputBindings>
  48.     <KeyBinding
  49.         Key="Esc"
  50.         Command="{Binding BackCommand}"
  51.         Modifiers="" />
  52. </Window.InputBindings>//获取新增、编辑页面接口
  53. <Window.InputBindings>
  54.     <KeyBinding
  55.         Key="Esc"
  56.         Command="{Binding BackCommand}"
  57.         Modifiers="" />
  58. </Window.InputBindings>var page = App.<strong>GetService<BatchAddDictDataPage></strong>();
  59. <Window.InputBindings>
  60.     <KeyBinding
  61.         Key="Esc"
  62.         Command="{Binding BackCommand}"
  63.         Modifiers="" />
  64. </Window.InputBindings>page!.ViewModel.DictType = this.ViewModel.SelectDictType;
  65. <Window.InputBindings>
  66.     <KeyBinding
  67.         Key="Esc"
  68.         Command="{Binding BackCommand}"
  69.         Modifiers="" />
  70. </Window.InputBindings>page!.ViewModel.Item = new BatchAddDictDataDto();
  71. <Window.InputBindings>
  72.     <KeyBinding
  73.         Key="Esc"
  74.         Command="{Binding BackCommand}"
  75.         Modifiers="" />
  76. </Window.InputBindings>//模态对话框打开
  77. <Window.InputBindings>
  78.     <KeyBinding
  79.         Key="Esc"
  80.         Command="{Binding BackCommand}"
  81.         Modifiers="" />
  82. </Window.InputBindings>page.<strong>ShowDialog</strong>();
  83. }
复制代码
通过GetService的方式获取新的一个窗口对象,并赋值对应的视图模型即可,然后打开模态对话框界面。
如果用户关闭,则会丢弃该对象,下次请求就是一个新的窗口实例了。
另外,我们窗口还可以配置快捷键ESC来关闭窗口,等同于按下关闭按钮的处理。我们在页面的Xaml文件中增加按键的绑定事件即可,如下代码所示。
  1. <Window.InputBindings>
  2.     <KeyBinding
  3.         Key="Esc"
  4.         Command="{Binding BackCommand}"
  5.         Modifiers="" />
  6. </Window.InputBindings>
复制代码
其中的BackCommand就是我们预设页面关闭的方法。
  1. <Window.InputBindings>
  2.     <KeyBinding
  3.         Key="Esc"
  4.         Command="{Binding BackCommand}"
  5.         Modifiers="" />
  6. </Window.InputBindings><Window.InputBindings>
  7.     <KeyBinding
  8.         Key="Esc"
  9.         Command="{Binding BackCommand}"
  10.         Modifiers="" />
  11. </Window.InputBindings>///<Window.InputBindings>
  12.     <KeyBinding
  13.         Key="Esc"
  14.         Command="{Binding BackCommand}"
  15.         Modifiers="" />
  16. </Window.InputBindings><Window.InputBindings>
  17.     <KeyBinding
  18.         Key="Esc"
  19.         Command="{Binding BackCommand}"
  20.         Modifiers="" />
  21. </Window.InputBindings> /// 关闭窗体<Window.InputBindings>
  22.     <KeyBinding
  23.         Key="Esc"
  24.         Command="{Binding BackCommand}"
  25.         Modifiers="" />
  26. </Window.InputBindings><Window.InputBindings>
  27.     <KeyBinding
  28.         Key="Esc"
  29.         Command="{Binding BackCommand}"
  30.         Modifiers="" />
  31. </Window.InputBindings>///<Window.InputBindings>
  32.     <KeyBinding
  33.         Key="Esc"
  34.         Command="{Binding BackCommand}"
  35.         Modifiers="" />
  36. </Window.InputBindings><Window.InputBindings>
  37.     <KeyBinding
  38.         Key="Esc"
  39.         Command="{Binding BackCommand}"
  40.         Modifiers="" />
  41. </Window.InputBindings> [RelayCommand]<Window.InputBindings>
  42.     <KeyBinding
  43.         Key="Esc"
  44.         Command="{Binding BackCommand}"
  45.         Modifiers="" />
  46. </Window.InputBindings><Window.InputBindings>
  47.     <KeyBinding
  48.         Key="Esc"
  49.         Command="{Binding BackCommand}"
  50.         Modifiers="" />
  51. </Window.InputBindings>private void Back()<Window.InputBindings>
  52.     <KeyBinding
  53.         Key="Esc"
  54.         Command="{Binding BackCommand}"
  55.         Modifiers="" />
  56. </Window.InputBindings><Window.InputBindings>
  57.     <KeyBinding
  58.         Key="Esc"
  59.         Command="{Binding BackCommand}"
  60.         Modifiers="" />
  61. </Window.InputBindings>{<Window.InputBindings>
  62.     <KeyBinding
  63.         Key="Esc"
  64.         Command="{Binding BackCommand}"
  65.         Modifiers="" />
  66. </Window.InputBindings><Window.InputBindings>
  67.     <KeyBinding
  68.         Key="Esc"
  69.         Command="{Binding BackCommand}"
  70.         Modifiers="" />
  71. </Window.InputBindings><Window.InputBindings>
  72.     <KeyBinding
  73.         Key="Esc"
  74.         Command="{Binding BackCommand}"
  75.         Modifiers="" />
  76. </Window.InputBindings>this.Close();<Window.InputBindings>
  77.     <KeyBinding
  78.         Key="Esc"
  79.         Command="{Binding BackCommand}"
  80.         Modifiers="" />
  81. </Window.InputBindings><Window.InputBindings>
  82.     <KeyBinding
  83.         Key="Esc"
  84.         Command="{Binding BackCommand}"
  85.         Modifiers="" />
  86. </Window.InputBindings>}
复制代码
对于通用的导入窗口,我们也是一样的处理方式,我们通过一些事件的定义,把一些实现逻辑放在调用类上实现也可以的。
  1. <Window.InputBindings>
  2.     <KeyBinding
  3.         Key="Esc"
  4.         Command="{Binding BackCommand}"
  5.         Modifiers="" />
  6. </Window.InputBindings>///<Window.InputBindings>
  7.     <KeyBinding
  8.         Key="Esc"
  9.         Command="{Binding BackCommand}"
  10.         Modifiers="" />
  11. </Window.InputBindings> /// 导出内容到Excel<Window.InputBindings>
  12.     <KeyBinding
  13.         Key="Esc"
  14.         Command="{Binding BackCommand}"
  15.         Modifiers="" />
  16. </Window.InputBindings>///<Window.InputBindings>
  17.     <KeyBinding
  18.         Key="Esc"
  19.         Command="{Binding BackCommand}"
  20.         Modifiers="" />
  21. </Window.InputBindings> [RelayCommand]<Window.InputBindings>
  22.     <KeyBinding
  23.         Key="Esc"
  24.         Command="{Binding BackCommand}"
  25.         Modifiers="" />
  26. </Window.InputBindings>private void ImportExcel()<Window.InputBindings>
  27.     <KeyBinding
  28.         Key="Esc"
  29.         Command="{Binding BackCommand}"
  30.         Modifiers="" />
  31. </Window.InputBindings>{<Window.InputBindings>
  32.     <KeyBinding
  33.         Key="Esc"
  34.         Command="{Binding BackCommand}"
  35.         Modifiers="" />
  36. </Window.InputBindings><Window.InputBindings>
  37.     <KeyBinding
  38.         Key="Esc"
  39.         Command="{Binding BackCommand}"
  40.         Modifiers="" />
  41. </Window.InputBindings>var page = App.GetService();<Window.InputBindings>
  42.     <KeyBinding
  43.         Key="Esc"
  44.         Command="{Binding BackCommand}"
  45.         Modifiers="" />
  46. </Window.InputBindings><Window.InputBindings>
  47.     <KeyBinding
  48.         Key="Esc"
  49.         Command="{Binding BackCommand}"
  50.         Modifiers="" />
  51. </Window.InputBindings>page!.ViewModel.Items?.Clear();<Window.InputBindings>
  52.     <KeyBinding
  53.         Key="Esc"
  54.         Command="{Binding BackCommand}"
  55.         Modifiers="" />
  56. </Window.InputBindings><Window.InputBindings>
  57.     <KeyBinding
  58.         Key="Esc"
  59.         Command="{Binding BackCommand}"
  60.         Modifiers="" />
  61. </Window.InputBindings>page!.ViewModel.TemplateFile = $"系统用户信息-模板.xls";[b]<Window.InputBindings>
  62.     <KeyBinding
  63.         Key="Esc"
  64.         Command="{Binding BackCommand}"
  65.         Modifiers="" />
  66. </Window.InputBindings><Window.InputBindings>
  67.     <KeyBinding
  68.         Key="Esc"
  69.         Command="{Binding BackCommand}"
  70.         Modifiers="" />
  71. </Window.InputBindings>page[/b][b]!.OnDataSave -= ExcelData_OnDataSave;<Window.InputBindings>
  72.     <KeyBinding
  73.         Key="Esc"
  74.         Command="{Binding BackCommand}"
  75.         Modifiers="" />
  76. </Window.InputBindings><Window.InputBindings>
  77.     <KeyBinding
  78.         Key="Esc"
  79.         Command="{Binding BackCommand}"
  80.         Modifiers="" />
  81. </Window.InputBindings>page!.OnDataSave += ExcelData_OnDataSave;<Window.InputBindings>
  82.     <KeyBinding
  83.         Key="Esc"
  84.         Command="{Binding BackCommand}"
  85.         Modifiers="" />
  86. </Window.InputBindings><Window.InputBindings>
  87.     <KeyBinding
  88.         Key="Esc"
  89.         Command="{Binding BackCommand}"
  90.         Modifiers="" />
  91. </Window.InputBindings>[/b]//模态对话框打开<Window.InputBindings>
  92.     <KeyBinding
  93.         Key="Esc"
  94.         Command="{Binding BackCommand}"
  95.         Modifiers="" />
  96. </Window.InputBindings><Window.InputBindings>
  97.     <KeyBinding
  98.         Key="Esc"
  99.         Command="{Binding BackCommand}"
  100.         Modifiers="" />
  101. </Window.InputBindings>page.ShowDialog();<Window.InputBindings>
  102.     <KeyBinding
  103.         Key="Esc"
  104.         Command="{Binding BackCommand}"
  105.         Modifiers="" />
  106. </Window.InputBindings>}
复制代码
例如上面红色部分的事件,我们就是放在调用类中差异化处理。

这样就可以差异化不同的内容,同时保留通用模块的灵活性,导入界面如下所示。

 

来源:https://www.cnblogs.com/wuhuacong/archive/2023/09/21/17720116.html
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x

举报 回复 使用道具