Advertisement
Guest User

Untitled

a guest
Mar 15th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.84 KB | None | 0 0
  1. <ListBox ItemsSource="{Binding SeriesCollection}" Name="lbSeries" Background="#6E7587" ItemContainerStyle="{StaticResource highlightStyle}" SelectionMode="Single" AlternationCount="2" Grid.Row="2" ScrollViewer.VerticalScrollBarVisibility="Auto">
  2. <ListBox.ItemTemplate>
  3. <DataTemplate>
  4. <Grid>
  5. <Grid.ColumnDefinitions>
  6. <ColumnDefinition Width="200" />
  7. <ColumnDefinition Width="100" />
  8. <ColumnDefinition Width="100" />
  9. <ColumnDefinition Width="100" />
  10. <ColumnDefinition Width="100" />
  11. <ColumnDefinition Width="*" />
  12. </Grid.ColumnDefinitions>
  13. <TextBox Margin="5,0" FontSize="14" Name="txtName" Text="{Binding Name}" Width="190" Height="30" Grid.Column="0" />
  14. <wpfToolkit:IntegerUpDown Value="{Binding Season}" Name="nudSeason" FontSize="14" Height="30" Width="95" Increment="1" Maximum="100000" Minimum="0" Grid.Column="1" />
  15. <wpfToolkit:IntegerUpDown Value="{Binding Episode}" Name="nudEpisode" FontSize="14" Height="30" Width="95" Increment="1" Maximum="100000" Minimum="0" Grid.Column="2" />
  16. <Button Command="{Binding Save}" Grid.Column="3" Width="60" Height="50" Cursor="Hand" ToolTip="Save program" VerticalAlignment="Center" HorizontalAlignment="Center">
  17. <Button.Template>
  18. <ControlTemplate>
  19. <Border HorizontalAlignment="Center" VerticalAlignment="Center" >
  20. <Image Source="ResourcesSave.png" Width="30" Height="40" />
  21. </Border>
  22. </ControlTemplate>
  23. </Button.Template>
  24. </Button>
  25. <Button Command="{Binding Delete}" Grid.Column="4" Width="60" Height="50" Cursor="Hand" CommandParameter="{Binding ElementName=lbSeries,Path=SelectedItem}" ToolTip="Remove program" VerticalAlignment="Top" HorizontalAlignment="Left" Margin="15,0">
  26. <Button.Template>
  27. <ControlTemplate>
  28. <Border HorizontalAlignment="Center" VerticalAlignment="Center" >
  29. <Image Source="ResourcesDelete.png" Width="30" Height="40" />
  30. </Border>
  31. </ControlTemplate>
  32. </Button.Template>
  33. </Button>
  34. <Label Content="{Binding Information}" Grid.Column="5" FontSize="14" Margin="10,0" />
  35. </Grid>
  36. </DataTemplate>
  37. </ListBox.ItemTemplate>
  38. </ListBox>
  39.  
  40. using System;
  41. using System.Collections.ObjectModel;
  42. using System.ComponentModel;
  43. using System.Data;
  44. using SeriesTracker.Functions;
  45. using System.Linq;
  46.  
  47. namespace SeriesTracker.Client
  48. {
  49. public class SeriesTrackerViewModel : INotifyPropertyChanged
  50. {
  51. public DelegateCommand NewSeries { get; set; }
  52.  
  53. public event PropertyChangedEventHandler PropertyChanged;
  54.  
  55. private ObservableCollection<Series> _series = new ObservableCollection<Series>();
  56. public ObservableCollection<Series> SeriesCollection
  57. {
  58. get { return _series; }
  59. set
  60. {
  61. _series = value;
  62. RaisePropertyChanged("SeriesCollection");
  63. }
  64. }
  65.  
  66. public SeriesTrackerViewModel()
  67. {
  68. NewSeries = new DelegateCommand(AddNewSeries);
  69.  
  70. DataTable table = DataAccessLayer.GetSeries();
  71. if (table.Rows.Count > 0)
  72. LoadSeries(table);
  73. }
  74.  
  75. private void LoadSeries(DataTable table)
  76. {
  77. foreach (DataRow row in table.Rows)
  78. {
  79. int id = Int32.Parse(row["Id"].ToString());
  80. string name = row["Name"].ToString();
  81.  
  82. int season = 0;
  83. int episode = 0;
  84.  
  85. if (Int32.TryParse(row["Season"].ToString(), out season) &&
  86. Int32.TryParse(row["Episode"].ToString(), out episode))
  87. {
  88. var series = new Series(id, name, season, episode);
  89. SeriesCollection.Add(series);
  90. }
  91. }
  92. }
  93.  
  94. public void AddNewSeries()
  95. {
  96. SeriesCollection.AddSeries();
  97. }
  98.  
  99. private void RaisePropertyChanged(string propertyName)
  100. {
  101. PropertyChangedEventHandler handler = PropertyChanged;
  102. if (handler != null)
  103. handler(this, new PropertyChangedEventArgs(propertyName));
  104. }
  105. }
  106. }
  107.  
  108. using System;
  109. using System.ComponentModel;
  110. using System.Collections.ObjectModel;
  111.  
  112. namespace SeriesTracker.Functions
  113. {
  114. public class Series : INotifyPropertyChanged
  115. {
  116. public event PropertyChangedEventHandler PropertyChanged;
  117.  
  118. private DelegateCommand _save;
  119. public DelegateCommand Save
  120. {
  121. get { return _save; }
  122. set
  123. {
  124. _save = value;
  125. RaisePropertyChanged("Save");
  126. }
  127. }
  128.  
  129. private DelegateCommand _delete;
  130. public DelegateCommand Delete
  131. {
  132. get{return _delete;}
  133. set
  134. {
  135. _delete = value;
  136. RaisePropertyChanged("Delete");
  137. }
  138. }
  139.  
  140. public int Id { get; set; }
  141.  
  142. string _name;
  143. public String Name
  144. {
  145. get { return _name; }
  146. set
  147. {
  148. _name = value;
  149. RaisePropertyChanged("Name");
  150. }
  151. }
  152.  
  153. int _season;
  154. public Int32 Season
  155. {
  156. get { return _season; }
  157. set
  158. {
  159. _season = value;
  160. RaisePropertyChanged("Season");
  161. }
  162. }
  163.  
  164. int _episode;
  165. public Int32 Episode
  166. {
  167. get { return _episode; }
  168. set
  169. {
  170. _episode = value;
  171. RaisePropertyChanged("Episode");
  172. }
  173. }
  174.  
  175. string _information;
  176. public String Information
  177. {
  178. get { return _information; }
  179. set
  180. {
  181. _information = value;
  182. RaisePropertyChanged("Information");
  183. }
  184. }
  185.  
  186. public Series(int id,string name,int season, int episode)
  187. {
  188. Id = id;
  189. Name = name;
  190. Season = season;
  191. Episode = episode;
  192.  
  193. Save = new DelegateCommand(SaveItem);
  194. Delete = new DelegateCommand(DeleteItem);
  195. }
  196.  
  197. public void DeleteItem()
  198. {
  199. var selectedItem = this;
  200. if (selectedItem.Id != -1)
  201. {
  202. DataAccessLayer.Delete(selectedItem.Id);
  203. }
  204. }
  205.  
  206. public void SaveItem()
  207. {
  208. var selectedItem = this;
  209. if (selectedItem.Id == -1)
  210. DataAccessLayer.AddEntry(selectedItem.Name,selectedItem.Season,selectedItem.Episode);
  211. else
  212. DataAccessLayer.Update(selectedItem.Id,
  213. selectedItem.Name,selectedItem.Season,selectedItem.Episode);
  214. }
  215.  
  216. private void RaisePropertyChanged(string propertyName)
  217. {
  218. PropertyChangedEventHandler handler = PropertyChanged;
  219. if (handler != null)
  220. handler(this, new PropertyChangedEventArgs(propertyName));
  221. }
  222. }
  223. }
  224.  
  225. DelegateCommand<Series> DeleteCommand { get; set; }
  226. DeleteCommand = new RelayCommand<Series>(OnDelete);
  227.  
  228. // This method will gets execute on click the of Delete Button1
  229. private void OnDelete(Series series)
  230. {
  231. }
  232.  
  233. private void LoadSeries(DataTable table)
  234. {
  235. ...
  236. {
  237. var series = new Series(id, name, season, episode);
  238. series.PropertyChanged += { RaisePropertyChanged("SeriesCollection"); }
  239. SeriesCollection.Add(series);
  240. }
  241. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement