Guest User

Untitled

a guest
Dec 15th, 2017
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.36 KB | None | 0 0
  1. <Window
  2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4. xmlns:ViewModel="clr-namespace:ABCD.ViewModel" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"
  5. x:Class="ABCD.SettingsWindow"
  6. x:Name="SettingsWindow1"
  7. Title="SettingsWindow"
  8. Width="640" Height="480">
  9.  
  10. <Window.Resources>
  11. <ViewModel:SettingsViewModel x:Key="SettingsViewModelDataSource" d:IsDataSource="True"/>
  12. </Window.Resources>
  13.  
  14. <Grid x:Name="LayoutRoot" DataContext="{Binding Source={StaticResource SettingsViewModelDataSource}}">
  15. <Grid.ColumnDefinitions>
  16. <ColumnDefinition Width="185*"/>
  17. <ColumnDefinition Width="131*"/>
  18. </Grid.ColumnDefinitions>
  19. <Button x:Name="addCurrencyButton" Content="Add Currency" HorizontalAlignment="Left" Height="20" Margin="290,120,0,0" VerticalAlignment="Top" Width="170" Click="addCurrencyButton_Click" Grid.ColumnSpan="2"/>
  20. <ComboBox x:Name="currencyNamesComboBox" HorizontalAlignment="Left" Height="25" Margin="50,120,0,0" VerticalAlignment="Top" Width="150" ItemsSource="{Binding CurrencyNames}"/>
  21. </Grid>
  22.  
  23. public class SettingsViewModel:INotifyPropertyChanged
  24. {
  25. private CurrencyConverter currencyConverter;
  26.  
  27. private ObservableCollection<String> currencyNames;
  28.  
  29. private ObservableCollection<Double> currencyRates;
  30.  
  31. public ObservableCollection<String> CurrencyNames
  32. {
  33. get { return currencyNames; }
  34. set { currencyNames = value; OnPropertyChanged("CurrencyNames"); }
  35. }
  36.  
  37.  
  38. public ObservableCollection<Double> CurrencyRates
  39. {
  40. get { return currencyRates; }
  41. set { currencyRates = value; OnPropertyChanged("CurrencyRates"); }
  42. }
  43.  
  44. public SettingsViewModel()
  45. {
  46.  
  47. this.CurrencyNames = new ObservableCollection<String>();
  48. this.CurrencyRates = new ObservableCollection<Double>();
  49. currencyConverter = new CurrencyConverter();
  50. UpdateCurrencySettings(currencyConverter.CurrencyNameToRates);
  51. }
  52.  
  53. public void AddCurrency(String currencyName, Double currencyRate)
  54. {
  55. currencyConverter.AddNewCurrency(currencyName, currencyRate);
  56. UpdateCurrencySettings(currencyConverter.CurrencyNameToRates);
  57.  
  58. }
  59.  
  60. public void RemoveCurrency(String currencyName)
  61. {
  62. currencyConverter.RemoveCurrency(currencyName);
  63. UpdateCurrencySettings(currencyConverter.CurrencyNameToRates);
  64. }
  65.  
  66. private void UpdateCurrencySettings(Dictionary<string, double> currencyNameToRates)
  67. {
  68. List<String> currencyNames = new List<String>();
  69. List<Double> currencyRates = new List<Double>();
  70.  
  71. foreach(String key in currencyNameToRates.Keys)
  72. {
  73. currencyNames.Add(key);
  74. currencyRates.Add(currencyNameToRates[key]);
  75. }
  76. this.CurrencyNames.Clear();
  77. this.CurrencyRates.Clear();
  78. currencyNames.ForEach(item => this.CurrencyNames.Add(item));
  79. currencyRates.ForEach(item => this.CurrencyRates.Add(item));
  80. }
  81.  
  82. public event PropertyChangedEventHandler PropertyChanged;
  83. // Create the OnPropertyChanged method to raise the event
  84. private void OnPropertyChanged(string propertyName)
  85. {
  86. if (PropertyChanged != null)
  87. {
  88. PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
  89. }
  90. }
  91. }
Add Comment
Please, Sign In to add comment