Guest User

Untitled

a guest
Aug 21st, 2018
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.88 KB | None | 0 0
  1. Toggle two views (Windows) in MVVM WPF
  2. /// <summary>
  3. /// Application_Startup
  4. /// </summary>
  5. /// <param name = "sender"></param>
  6. /// <param name = "e"></param>
  7. private void Application_Startup(object sender, StartupEventArgs e)
  8. {
  9. log.Debug("Application_Startup " + Utility.FUNCTION_ENTERED_LOG);
  10. try
  11. {
  12.  
  13. if (e.Args.Length == 0)
  14. {
  15. AboutDialog.SpashScreen(Utility.TOOL_NAME,
  16. Utility.TOOL_VERSION);
  17. MainView mainForm = new MainView();
  18. mainForm.ShowDialog();
  19. }
  20. else
  21. {
  22. string key = null;
  23. foreach (string arg in e.Args)
  24. {
  25. if (arg.StartsWith("-"))
  26. {
  27. //this is a key
  28. key = arg;
  29. if (key.Equals("-config"))
  30. {
  31. CommandLineArgs.Add(key, "config");
  32. break;
  33. }
  34. if (key.Equals("-start"))
  35. {
  36. CommandLineArgs.Add(key, "start");
  37. }
  38. }
  39. else
  40. {
  41. //should be a value
  42. if (key == null)
  43. {
  44. throw new Exception(
  45. "The command line arguments are malformed.");
  46. }
  47. CommandLineArgs.Add(key, arg);
  48. key = null;
  49. }
  50. }
  51. string config = string.Empty;
  52. foreach (object k in App.CommandLineArgs.Keys)
  53. {
  54. config += CommandLineArgs[k].ToString();
  55. }
  56.  
  57. switch (config)
  58. {
  59. case "config":
  60. AboutDialog.SpashScreen(
  61. Utility.TOOL_NAME,
  62. Utility.TOOL_VERSION);
  63. MainView mainForm = new MainView();
  64. mainForm.ShowDialog();
  65. break;
  66. case "start" :
  67. ExecutionForm execuForm= new ExecutionForm();
  68. execuForm.ShowDialog();
  69. break;
  70. default:
  71. MessageBox.Show("Incorrect Parameters",
  72. Utility.TOOL_NAME);
  73. Application.Current.Shutdown();
  74. break;
  75. }
  76.  
  77. }
  78. log.Debug("Application_Startup" + Utility.FUNCTION_EXIT_LOG);
  79. }
  80. catch (Exception ex)
  81. {
  82. log.Error("Application_Startup" + ex.Message, ex);
  83. }
  84. }
  85.  
  86. <DockPanel LastChildFill="true">
  87. <Menu DockPanel.Dock="Top" ...>
  88. </Menu>
  89. <ContentControl Content="{Binding}" ... />
  90. </DockPanel>
  91.  
  92. <Window.Resources>
  93. <DataTemplate x:Key="ConfigView" ... />
  94. <DataTemplate x:Key="ExecutionView" ... />
  95. </Window.Resources>
  96.  
  97. public class ViewModel : INotifPropertyChanged
  98. {
  99. private string mode;
  100.  
  101. public string Mode
  102. {
  103. get { return mode; }
  104. set { mode = value; NotifPropertyChanged("Mode"); }
  105. }
  106.  
  107. private IComamnd changeModeCommand;
  108.  
  109. public ICommand ChangeModeCommand
  110. {
  111. get
  112. {
  113. if (changeModeCommand == null)
  114. changeModeCommand
  115. = new DelegateCommand(
  116. OnModeChange,
  117. CanModeChange);
  118. return changeModeCommand;
  119. }
  120. }
  121.  
  122. //// Other properties and functions go here ...
  123. }
  124.  
  125. <Window.InputBindings>
  126. <KeyBinding Command="{Binding ChangeModeCommand}"
  127. Key="F12" />
  128. </Window.InputBindings>
  129.  
  130. <ContentControl Content="{Binding}">
  131. <ContentControl.Style>
  132. <Style TargetType="{x:Type ContentControl}">
  133. <Style.Triggers>
  134. <DataTrigger Binding="{Binding Mode}" Value="Config">
  135. <Setter Property="ContentTemplate"
  136. Value="{StaticResource ConfigView}"/>
  137. </DataTrigger>
  138. <DataTrigger Binding="{Binding Mode}" Value="Execution">
  139. <Setter Property="ContentTemplate"
  140. Value="{StaticResource ExecutionView}"/>
  141. </DataTrigger>
  142. </Style.Triggers>
  143. </Style>
  144. </ContentControl.Style>
  145. </ContentControl>
Add Comment
Please, Sign In to add comment