Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <Window
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- 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"
- x:Class="ABCD.SettingsWindow"
- x:Name="SettingsWindow1"
- Title="SettingsWindow"
- Width="640" Height="480">
- <Window.Resources>
- <ViewModel:SettingsViewModel x:Key="SettingsViewModelDataSource" d:IsDataSource="True"/>
- </Window.Resources>
- <Grid x:Name="LayoutRoot" DataContext="{Binding Source={StaticResource SettingsViewModelDataSource}}">
- <Grid.ColumnDefinitions>
- <ColumnDefinition Width="185*"/>
- <ColumnDefinition Width="131*"/>
- </Grid.ColumnDefinitions>
- <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"/>
- <ComboBox x:Name="currencyNamesComboBox" HorizontalAlignment="Left" Height="25" Margin="50,120,0,0" VerticalAlignment="Top" Width="150" ItemsSource="{Binding CurrencyNames}"/>
- </Grid>
- public class SettingsViewModel:INotifyPropertyChanged
- {
- private CurrencyConverter currencyConverter;
- private ObservableCollection<String> currencyNames;
- private ObservableCollection<Double> currencyRates;
- public ObservableCollection<String> CurrencyNames
- {
- get { return currencyNames; }
- set { currencyNames = value; OnPropertyChanged("CurrencyNames"); }
- }
- public ObservableCollection<Double> CurrencyRates
- {
- get { return currencyRates; }
- set { currencyRates = value; OnPropertyChanged("CurrencyRates"); }
- }
- public SettingsViewModel()
- {
- this.CurrencyNames = new ObservableCollection<String>();
- this.CurrencyRates = new ObservableCollection<Double>();
- currencyConverter = new CurrencyConverter();
- UpdateCurrencySettings(currencyConverter.CurrencyNameToRates);
- }
- public void AddCurrency(String currencyName, Double currencyRate)
- {
- currencyConverter.AddNewCurrency(currencyName, currencyRate);
- UpdateCurrencySettings(currencyConverter.CurrencyNameToRates);
- }
- public void RemoveCurrency(String currencyName)
- {
- currencyConverter.RemoveCurrency(currencyName);
- UpdateCurrencySettings(currencyConverter.CurrencyNameToRates);
- }
- private void UpdateCurrencySettings(Dictionary<string, double> currencyNameToRates)
- {
- List<String> currencyNames = new List<String>();
- List<Double> currencyRates = new List<Double>();
- foreach(String key in currencyNameToRates.Keys)
- {
- currencyNames.Add(key);
- currencyRates.Add(currencyNameToRates[key]);
- }
- this.CurrencyNames.Clear();
- this.CurrencyRates.Clear();
- currencyNames.ForEach(item => this.CurrencyNames.Add(item));
- currencyRates.ForEach(item => this.CurrencyRates.Add(item));
- }
- public event PropertyChangedEventHandler PropertyChanged;
- // Create the OnPropertyChanged method to raise the event
- private void OnPropertyChanged(string propertyName)
- {
- if (PropertyChanged != null)
- {
- PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
- }
- }
- }
Add Comment
Please, Sign In to add comment