Verwenden der portablen Klassenbibliothek mit Model-View-View Model
-
System.Collections.ObjectModel.ObservableCollection<T> -Klasse -
System.Collections.ObjectModel.ReadOnlyObservableCollection<T> -Klasse -
System.Collections.Specialized.INotifyCollectionChanged -Klasse -
System.Collections.Specialized.NotifyCollectionChangedAction -Klasse -
System.Collections.Specialized.NotifyCollectionChangedEventArgs -Klasse -
System.Collections.Specialized.NotifyCollectionChangedEventHandler -Klasse -
System.Windows.Input.ICommand -Klasse -
Alle Klassen im System.ComponentModel.DataAnnotations-Namespace
Modell
using System; using System.Collections.Generic; using System.Linq; namespace SimpleMVVM.Model { public class CustomerRepository { private List<Customer> _customers; public CustomerRepository() { _customers = new List<Customer> { new Customer(){ CustomerID = 1, FullName="Dana Birkby", Phone="394-555-0181"}, new Customer(){ CustomerID = 2, FullName="Adriana Giorgi", Phone="117-555-0119"}, new Customer(){ CustomerID = 3, FullName="Wei Yu", Phone="798-555-0118"} }; } public List<Customer> GetCustomers() { return _customers; } public void UpdateCustomer(Customer SelectedCustomer) { Customer customerToChange = _customers.Single(c => c.CustomerID == SelectedCustomer.CustomerID); customerToChange = SelectedCustomer; } } }
Ansichtsmodell
using System; using System.ComponentModel; namespace SimpleMVVM.ViewModel { public abstract class ViewModelBase : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; protected virtual void OnPropertyChanged(string propName) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(propName)); } } } }
using System; using System.Windows.Input; namespace SimpleMVVM.ViewModel { public class RelayCommand : ICommand { private readonly Action _handler; private bool _isEnabled; public RelayCommand(Action handler) { _handler = handler; } public bool IsEnabled { get { return _isEnabled; } set { if (value != _isEnabled) { _isEnabled = value; if (CanExecuteChanged != null) { CanExecuteChanged(this, EventArgs.Empty); } } } } public bool CanExecute(object parameter) { return IsEnabled; } public event EventHandler CanExecuteChanged; public void Execute(object parameter) { _handler(); } } }
using System; using System.Collections.Generic; using SimpleMVVM.Model; namespace SimpleMVVM.ViewModel { public class CustomerViewModel : ViewModelBase { private List<Customer> _customers; private Customer _currentCustomer; private CustomerRepository _repository; public CustomerViewModel() { _repository = new CustomerRepository(); _customers = _repository.GetCustomers(); WireCommands(); } private void WireCommands() { UpdateCustomerCommand = new RelayCommand(UpdateCustomer); } public RelayCommand UpdateCustomerCommand { get; private set; } public List<Customer> Customers { get { return _customers; } set { _customers = value; } } public Customer CurrentCustomer { get { return _currentCustomer; } set { if (_currentCustomer != value) { _currentCustomer = value; OnPropertyChanged("CurrentCustomer"); UpdateCustomerCommand.IsEnabled = true; } } } public void UpdateCustomer() { _repository.UpdateCustomer(CurrentCustomer); } } }
Ansicht
<Window x:Class="SimpleWPF.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:viewModels="clr-namespace:SimpleMVVM.ViewModel;assembly=SimpleMVVM.ViewModel" Title="MainWindow" Height="350" Width="525"> <Window.Resources> <viewModels:MainPageViewModel x:Key="ViewModel" /> </Window.Resources> <Grid DataContext="{Binding Source={StaticResource ViewModel}}"> <Grid.ColumnDefinitions> <ColumnDefinition></ColumnDefinition> <ColumnDefinition></ColumnDefinition> </Grid.ColumnDefinitions> <Grid.RowDefinitions> <RowDefinition Height="Auto"></RowDefinition> <RowDefinition Height="Auto"></RowDefinition> <RowDefinition Height="Auto"></RowDefinition> <RowDefinition Height="Auto"></RowDefinition> <RowDefinition Height="Auto"></RowDefinition> </Grid.RowDefinitions> <TextBlock Height="23" Margin="5" HorizontalAlignment="Right" Grid.Column="0" Grid.Row="0" Name="textBlock2" Text="Select a Customer:" VerticalAlignment="Top" /> <ComboBox Height="23" HorizontalAlignment="Left" Grid.Column="1" Grid.Row="0" Name="CustomersComboBox" VerticalAlignment="Top" Width="173" DisplayMemberPath="FullName" SelectedItem="{Binding Path=CurrentCustomer, Mode=TwoWay}" ItemsSource="{Binding Path=Customers}" /> <TextBlock Height="23" Margin="5" HorizontalAlignment="Right" Grid.Column="0" Grid.Row="1" Name="textBlock4" Text="Customer ID" /> <TextBlock Height="23" Margin="5" HorizontalAlignment="Right" Grid.Column="0" Grid.Row="2" Name="textBlock5" Text="Name" /> <TextBlock Height="23" Margin="5" HorizontalAlignment="Right" Grid.Column="0" Grid.Row="3" Name="textBlock9" Text="Phone" /> <TextBlock Height="23" HorizontalAlignment="Left" Grid.Column="1" Grid.Row="1" Name="CustomerIDTextBlock" Text="{Binding ElementName=CustomersComboBox, Path=SelectedItem.CustomerID}" /> <TextBox Height="23" HorizontalAlignment="Left" Grid.Column="1" Grid.Row="2" Width="219" Text="{Binding Path=CurrentCustomer.FullName, Mode=TwoWay}" /> <TextBox Height="23" HorizontalAlignment="Left" Grid.Column="1" Grid.Row="3" Width="219" Text="{Binding Path=CurrentCustomer.Phone, Mode=TwoWay}" /> <Button Command="{Binding UpdateCustomerCommand}" Content="Update" Height="23" HorizontalAlignment="Right" Grid.Column="0" Grid.Row="4" Name="UpdateButton" VerticalAlignment="Top" Width="75" /> </Grid> </Window>