2015-04-15 104 views
0

我正試圖顯示一個模型,並且當用戶單擊一個按鈕時我想保存它。 (將ModelView OneWay綁定到Model,如果按下保存按鈕,我手動更新源代碼。) 這可能嗎?在Viewmodel中保存視圖的更改?

在這裏,我創建了一個小例子,解決了我的問題。 (不MVVM!)

MainWindow.xaml

<Grid> 
     <Grid.DataContext> 
      <ViewModel:MainViewModel/> 
     </Grid.DataContext> 
     <Grid.ColumnDefinitions> 
      <ColumnDefinition/> 
      <ColumnDefinition/> 
     </Grid.ColumnDefinitions> 
     <local:PersonListView Grid.Column="0" DataContext="{Binding ViewModel_PersonList}"/> 
     <local:PersonView Grid.Column="1" DataContext="{Binding ViewModel_Person}"/> 
    </Grid> 

PersonListView.xaml

<ListView ItemsSource="{Binding PersonList}" SelectedItem="{Binding SelectedPerson}"> 
     <ListView.ItemTemplate> 
      <DataTemplate> 
       <TextBlock Text="{Binding LastName}"/> 
      </DataTemplate> 
     </ListView.ItemTemplate> 
    </ListView> 

PersonView.xaml

<Grid DataContext="{Binding}"> 
    <Grid.ColumnDefinitions> 
     <ColumnDefinition/> 
     <ColumnDefinition/> 
    </Grid.ColumnDefinitions> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="auto"/> 
     <RowDefinition Height="auto"/> 
     <RowDefinition Height="auto"/> 
     <RowDefinition/> 
     <RowDefinition Height="auto"/> 
    </Grid.RowDefinitions> 
    <TextBlock Text="First Name:" Grid.Column="0" Grid.Row="0" Margin="5"/> 
    <TextBlock Text="Last Name:" Grid.Column="0" Grid.Row="1" Margin="5"/> 
    <TextBlock Text="Age:" Grid.Column="0" Grid.Row="2" Margin="5"/> 
    <TextBox Text="{Binding Path=Person.FirstName, UpdateSourceTrigger=Explicit}" Grid.Column="1" Grid.Row="0" Margin="5"/> 
    <TextBox Text="{Binding Path=Person.LastName, UpdateSourceTrigger=Explicit}" Grid.Column="1" Grid.Row="1" Margin="5"/> 
    <TextBox Text="{Binding Path=Person.Age, UpdateSourceTrigger=Explicit}" Grid.Column="1" Grid.Row="2" Margin="5"/> 
    <Button Content="Save" Command="{Binding SaveCommand}" Grid.Column="1" Grid.Row="4"/> 
</Grid> 

Person.cs

public class Person : INotifyPropertyChanged 
    { 
     private string firstName; 
     public string FirstName 
     { 
      get { return firstName; } 
      set 
      { 
       firstName = value; 
       OnPropertyChanged("FirstName"); 
      } 
     } 

     private string lastName; 
     public string LastName 
     { 
      get { return lastName; } 
      set 
      { 
       lastName = value; 
       OnPropertyChanged("LastName"); 
      } 
     } 

     private int age; 
     public int Age 
     { 
      get { return age; } 
      set 
      { 
       age = value; 
       OnPropertyChanged("Age"); 
      } 
     } 

     public event PropertyChangedEventHandler PropertyChanged; 

     private void OnPropertyChanged(string propertyName) 
     { 
      if (PropertyChanged != null) 
       PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
     } 
    } 

MainViewModel.cs

public class MainViewModel : ViewModelBase 
    { 
     /// <summary> 
     /// Initializes a new instance of the MainViewModel class. 
     /// </summary> 
     public MainViewModel() 
     { 
      ViewModel_PersonList.PersonList.Add(new Model.Person() 
      { 
       LastName = "Test", 
       FirstName = "John", 
       Age = 20 
      }); 
      ViewModel_PersonList.PersonList.Add(new Model.Person() 
      { 
       LastName = "Example", 
       FirstName = "Luke", 
       Age = 25 
      }); 
      ViewModel_PersonList.SelectedPerson = ViewModel_PersonList.PersonList[1]; 

      ViewModel_Person.Person = ViewModel_PersonList.SelectedPerson; 
     } 

     ViewModel.PersonViewModel _ViewModel_Person = new ViewModel.PersonViewModel(); 
     public ViewModel.PersonViewModel ViewModel_Person 
     { 
      get 
      { 
       return _ViewModel_Person; 
      } 
      set 
      { 
       _ViewModel_Person = value; 
       RaisePropertyChanged("ViewModel_Person"); 
      } 
     } 

     ViewModel.PersonListViewModel _ViewModel_PersonList = new ViewModel.PersonListViewModel(); 
     public ViewModel.PersonListViewModel ViewModel_PersonList 
     { 
      get 
      { 
       return _ViewModel_PersonList; 
      } 
      set 
      { 
       _ViewModel_PersonList = value; 
       RaisePropertyChanged("ViewModel_PersonList"); 
      } 
     } 
    } 

PersonListViewModel.css

public class PersonListViewModel : ViewModelBase 
    { 
     ObservableCollection<Model.Person> _PersonList = new ObservableCollection<Model.Person>(); 
     public ObservableCollection<Model.Person> PersonList 
     { 
      get 
      { 
       return _PersonList; 
      } 
      set 
      { 
       _PersonList = value; 
       RaisePropertyChanged("PersonList"); 
      } 
     } 

     Model.Person _SelectedPerson = new Model.Person(); 
     public Model.Person SelectedPerson 
     { 
      get 
      { 
       return _SelectedPerson; 
      } 
      set 
      { 
       MessengerInstance.Send<Model.Person>(value); 
       _SelectedPerson = value; 
       RaisePropertyChanged("SelectedPerson"); 
      } 
     } 
    } 

PersonViewModel.cs

public class PersonViewModel : ViewModelBase 
    { 
     public PersonViewModel() 
     { 
      MessengerInstance.Register<Model.Person>(this, per => Person = per); 
     } 

     Model.Person _Person = new Model.Person(); 
     public Model.Person Person 
     { 
      get 
      { 
       return _Person; 
      } 
      set 
      { 
       _Person = value; 
       RaisePropertyChanged("Person"); 
      } 
     } 

     private RelayCommand<Model.Person> _SaveCommand; 
     public ICommand SaveCommand 
     { 
      get 
      { 
       if (_SaveCommand == null) 
        _SaveCommand = new RelayCommand<Model.Person>(SaveCommand_Execute, SaveCommand_CanExecute); 
       return _SaveCommand; 
      } 
     } 

     private bool SaveCommand_CanExecute(Model.Person newPerson) 
     { 
      return true; 
     } 

     private void SaveCommand_Execute(Model.Person newPerson) 
     { 
// Do my saving! 
     } 
    } 

謝謝!

回答

2

您可以使Mode = TwoWay並使用UpdateSourceTrigger = Explicit。請參閱以下代碼。

<StackPanel> 
     <TextBox Name="tb_1" Text="HelloWorld!"/> 
     <TextBox Name="tb_2" Text="{Binding Path=Text, ElementName=tb_1, Mode=TwoWay,UpdateSourceTrigger=Explicit}"/> 
     <Button Content="Update Source" Click="Button_Click"/> 
    </StackPanel> 
+0

謝謝! 事情可以如此簡單,如果你知道解決方案! – MisterPresident

+0

你知道如何在視圖模型中實現這一點嗎? – MisterPresident

+0

我會在幾分鐘內發帖 –