还是可以改变对象的价值

时间:2015-03-24 08:08:18

标签: c# wpf mvvm datagrid

我正在尝试使用mvvm light框架并构建一个非常简单的应用程序。在应用程序中,您可以创建一个人并添加到数据网格。

App mainviewmodel看起来像

public class MainViewModel : ViewModelBase
    {
        //private readonly IDataService _dataService;
        private readonly IPersonService _personService;

        private ObservableCollection<Person> _persons = new ObservableCollection<Person>();

        public ObservableCollection<Person> Persons
        {
            get { return _persons; }
            set
            {
                _persons = value;
                RaisePropertyChanged("Persons");
            } 
        }

        Person _personInfo;

        public Person PersonInfo
        {
            get { return _personInfo; }
            set
            {
                _personInfo = value;
                RaisePropertyChanged("PersonInfo");
            }
        }

        #region Command Object Declarations

        public RelayCommand QueryPersons { get; set; }
        public RelayCommand<Person> AddPerson { get; set; }


        #endregion

        /// <summary>
        /// Gets the WelcomeTitle property.
        /// Changes to that property's value raise the PropertyChanged event. 
        /// </summary>
        public string WelcomeTitle
        {
            get
            {
                return "Simple MVVM";
            }

        }

        /// <summary>
        /// Initializes a new instance of the MainViewModel class.
        /// </summary>

        public MainViewModel(IPersonService personService)
        {
            _personService = personService;
            PersonInfo = new Person();
            QueryPersons = new RelayCommand(_GetPersons);
            AddPerson = new RelayCommand<Person>(_AddPerson);
        }


        void _GetPersons()
        {
            Persons.Clear();
            foreach (var p in _personService.GetPersons())
            {
                Persons.Add(p);
                RaisePropertyChanged("PersonInfo");
            }
        }

        void _AddPerson(Person p)
        {
            _personService.AddPerson(p);
            _GetPersons();

        }
}

您可以添加新用户的视图如下所示:

<UserControl x:Class="MvvmLight1.Views.AddPersonView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d"
             DataContext="{Binding Main, Source={StaticResource Locator}}"
             d:DesignHeight="300" d:DesignWidth="400">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="55"/>
            <RowDefinition Height="55"/>
            <RowDefinition Height="55"/>
            <RowDefinition Height="55"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="150"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
        <TextBlock Style="{StaticResource TextBlockStyle}" Grid.Row="0" TextWrapping="Wrap" Text="Name:"/>
        <TextBlock Style="{StaticResource TextBlockStyle}" Grid.Row="1" TextWrapping="Wrap" Text="Surname:"/>
        <TextBlock Style="{StaticResource TextBlockStyle}" Grid.Row="2" TextWrapping="Wrap" Text="Gender:"/>
        <TextBlock Style="{StaticResource TextBlockStyle}" Grid.Row="3" TextWrapping="Wrap" Text="Street:"/>

        <TextBox Style="{StaticResource TextBoxStyle}" Grid.Column="1" Grid.Row="0" TextWrapping="Wrap" Text="{Binding PersonInfo.Name,Mode=TwoWay}"/>
        <TextBox Style="{StaticResource TextBoxStyle}" Grid.Column="1" Grid.Row="1" TextWrapping="Wrap" Text="{Binding PersonInfo.Surname,Mode=TwoWay}"/>
        <TextBox Style="{StaticResource TextBoxStyle}" Grid.Column="1" Grid.Row="2" TextWrapping="Wrap" Text="{Binding PersonInfo.Gender,Mode=TwoWay}"/>
        <TextBox Style="{StaticResource TextBoxStyle}" Grid.Column="1" Grid.Row="3" TextWrapping="Wrap" Text="{Binding PersonInfo.Street,Mode=TwoWay}"/>

        <Button Grid.Row="4" Grid.ColumnSpan="2" FontSize="26" Command="{Binding AddPerson}" CommandParameter="{Binding PersonInfo}">Add person</Button>

    </Grid>
</UserControl>

我的问题是,在添加用户之后,我仍然可以更改datagrid中的添加值。图片将澄清,我的意思是: App2 并更改文本框中的值: enter image description here

正如您所看到的,我仍然可以更改添加的人的值,我该如何避免?

2 个答案:

答案 0 :(得分:1)

在Persons集合中添加PersonInfo后,您可以将该属性重置为新实例。

void _AddPerson(Person p)
{
    _personService.AddPerson(p);
    PersonInfo = new Person();
    _GetPersons();

}

答案 1 :(得分:1)

使用您的服务成功添加新人后,为PersonInfo分配新值。

void _AddPerson(Person p){
  _personService.AddPerson(p);
  PersonInfo = new Person();
  _GetPersons();
}

之后,输入字段将被清除,您可以创建新用户。