Advertisement
Guest User

Events in WPF

a guest
May 26th, 2019
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.32 KB | None | 0 0
  1. Background: This is code lifted from an application i made for a company to automate a bunch of calculations and device selections based on a bunch of data regarding a building. I'll be using the surface area of the building as an example.
  2.  
  3. I've tried to implement the MVVM model as best i could (explanations on it are kinda vague) so we have:
  4.  
  5.  
  6. A XAML page with say, a textfield and a button: (Note, this is from a page called Offertetool.XAML, see below)
  7. <TextBox Text="{Binding surface, UpdateSourceTrigger=PropertyChanged}"/>
  8. <Button Content="Calculate" Command="{Binding CalculateClick}"/>
  9.  
  10. A class that uses the data:
  11. public class Building
  12. {
  13. public double surface; //m^2
  14. public double twiceSurface;
  15.  
  16. public void CalculateDoubleSurfaceArea()
  17. {
  18. twiceSurface = surface*2;
  19. }
  20. }
  21.  
  22.  
  23. A class that does nothing but receive input from the UI and do preparations for processing, in this case ProjectHandler:
  24.  
  25. internal class ProjectHandler : ObservableObject
  26. {
  27. Building thisBuilding = new Building();
  28.  
  29. public string surface
  30. {
  31. get { return thisBuilding.surface.ToString(); }
  32. set
  33. {
  34. if (Double.TryParse(value, out double surfaceParse))
  35. {
  36. thisBuilding.surface = surfaceParse;
  37. OnPropertyChanged("oppervlakte");
  38. }
  39. }
  40. }
  41.  
  42. public ICommand CalculateClick
  43. {
  44. get { return new DelegateCommand(CalculateStuff); }
  45. }
  46.  
  47. public void CalculateStuff{
  48. thisBuilding.CalculateDoubleSurfaceArea();
  49. }
  50.  
  51. }
  52.  
  53. The ProjectHandler class implements ObservableObject, this is where my copy and pasting came in. I've lifted this class straight from an MVVM tutorial, it basically sets a class up to be able to use the events called by the XAML.
  54.  
  55. public abstract class ObservableObject : INotifyPropertyChanged
  56. {
  57. public event PropertyChangedEventHandler PropertyChanged;
  58.  
  59. protected void OnPropertyChanged(string propertyName)
  60. {
  61. if (PropertyChanged != null)
  62. {
  63. PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
  64. }
  65. }
  66. }
  67.  
  68. The button clicker event uses something called DelegateCommand, again, this is copy pasted:
  69.  
  70. class DelegateCommand : ICommand
  71. {
  72. private readonly Action action;
  73.  
  74. public DelegateCommand(Action _action)
  75. {
  76. action = _action;
  77. }
  78.  
  79. public void Execute(object parameter)
  80. {
  81. action();
  82. }
  83.  
  84. public bool CanExecute(object parameter) //Dit mogelijk nog aanpassen want nu is geen enkele knop ooit greyed out.
  85. {
  86. return true;
  87. }
  88. //#pragma warning disable 67
  89. public event EventHandler CanExecuteChanged;
  90. //#pragma warning restore 67
  91. }
  92.  
  93.  
  94. In order for the above to work, we need to point Offertetool.XAML.cs to the right datacontext, so:
  95. public partial class Offertetool : Page
  96. {
  97.  
  98. public Offertetool()
  99. {
  100. InitializeComponent();
  101. App thisApp = (App)Application.Current;
  102. this.DataContext = thisApp.projectHandler; //<-- in App.XAML.cs the instance of ProjectHandler is created.
  103. }
  104.  
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement