Tag: WPF MVVM WPFのためのMVVMデザイン パターン
public ICommand SaveCommand { get { if (_saveCommand == null) { _saveCommand = new RelayCommand( param => this.Save(), param => this.CanSave ); } return _saveCommand; } }
<!-- SAVE BUTTON --> <Button Grid.Row="8" Grid.Column="2" Command="{Binding Path=SaveCommand}" Content="_Save" HorizontalAlignment="Right" Margin="4,2" MinWidth="60" />
// In ViewModelBase.cs public event PropertyChangedEventHandler PropertyChanged; protected virtual void OnPropertyChanged(string propertyName) { this.VerifyPropertyName(propertyName); PropertyChangedEventHandler handler = this.PropertyChanged; if (handler != null) { var e = new PropertyChangedEventArgs(propertyName); handler(this, e); } } [Conditional("DEBUG")] [DebuggerStepThrough] public void VerifyPropertyName(string propertyName) { // Verify that the property name matches a real, // public, instance property on this object. if (TypeDescriptor.GetProperties(this)[propertyName] == null) { string msg = "Invalid property name: " + propertyName; if (this.ThrowOnInvalidPropertyName) throw new Exception(msg); else Debug.Fail(msg); } }
public string Email { get { return _customer.Email; } set { if (value == _customer.Email) return; _customer.Email = value; base.OnPropertyChanged("Email"); } }
public ReadOnlyCollection<CommandViewModel> Commands { get { if (_commands == null) { List<CommandViewModel> cmds = this.CreateCommands(); _commands = new ReadOnlyCollection<CommandViewModel>(cmds); } return _commands; } } List<CommandViewModel> CreateCommands() { return new List<CommandViewModel> { new CommandViewModel( Strings.MainWindowViewModel_Command_ViewAllCustomers, new RelayCommand(param => this.ShowAllCustomers())), new CommandViewModel( Strings.MainWindowViewModel_Command_CreateNewCustomer, new RelayCommand(param => this.CreateNewCustomer())) }; }
<DataTemplate x:Key="CommandsTemplate"> <ItemsControl IsTabStop="False" ItemsSource="{Binding}" Margin="6,2"> <ItemsControl.ItemTemplate> <DataTemplate> <TextBlock Margin="2,6"> <Hyperlink Command="{Binding Path=Command}"> <TextBlock Text="{Binding Path=DisplayName}" /> </Hyperlink> </TextBlock> </DataTemplate> </ItemsControl.ItemTemplate> </ItemsControl> </DataTemplate>
EventHandler handler = null; handler = delegate { viewModel.RequestClose -= handler; window.Close(); }; viewModel.RequestClose += handler;
ちなみに、タブ生成時は次のコードでタブの順番を制御している。TabControlを使っているのでバインドされているWorkspacesが変更されるだけで自動的にタブは増える。
void SetActiveWorkspace(WorkspaceViewModel workspace) { Debug.Assert(this.Workspaces.Contains(workspace)); ICollectionView collectionView = CollectionViewSource.GetDefaultView(this.Workspaces); if (collectionView != null) collectionView.MoveCurrentTo(workspace); }
※Bea Stollnitz » WPF’s CollectionViewSource
public string[] CustomerTypeOptions { get { if (_customerTypeOptions == null) { _customerTypeOptions = new string[] { "(Not Specified)", "Person", "Company" }; } return _customerTypeOptions; } } public string CustomerType { get { return _customerType; } set { if (value == _customerType || String.IsNullOrEmpty(value)) return; _customerType = value; if (_customerType == "Company") { _customer.IsCompany = true; } else if (_customerType == "Person") { _customer.IsCompany = false; } base.OnPropertyChanged("CustomerType"); base.OnPropertyChanged("LastName"); } }
public ObservableCollection<CustomerViewModel> AllCustomers { get; private set; }
void OnCustomerAddedToRepository(object sender, CustomerAddedEventArgs e) { var viewModel = new CustomerViewModel(e.NewCustomer, _customerRepository); this.AllCustomers.Add(viewModel); }
void OnCustomerAddedToRepository(object sender, CustomerAddedEventArgs e) { var viewModel = new CustomerViewModel(e.NewCustomer, _customerRepository); this.AllCustomers.Add(viewModel); }