在Viewmodel中保存视图的更改?

时间:2015-04-15 18:49:18

标签: c# wpf binding oneway

我试图显示模型,我想在用户点击按钮时保存它。 (将ModelView OneWay绑定到Model,如果按下了保存按钮,我会手动更新源。)  这可能吗?

这里我用我的问题创建了一个小例子。 (不是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!
        }
    }

谢谢!

1 个答案:

答案 0 :(得分: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>