1

我有一个模型

    public class PersonModel : INotifyPropertyChanged
    { 
        private string _firstname;
        public string FirstName 
        { 
            get {return _firstname; }
            set { _firstname = value; RaisePropertyChanged("FirstName"); } 
        }

        private string _lastname;
        public string LastName
        {
            get { return _lastname; }
            set { _lastname = value; RaisePropertyChanged("LastName"); }
        } 

        public string FullName 
        {
            get { return string.Format("{0} {1}", FirstName, LastName); } 
        }


        protected void RaisePropertyChanged(string propertyName)
        {
            var handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
    } 

我有一个视图模型:

 public class PersonViewModel
    {
        public ObservableCollection<PersonModel> Person { get; set; }
        public PersonViewModel()
        {
            Person = CookPersonData();
        }

        internal static ObservableCollection<PersonModel> CookPersonData()
        {
            ObservableCollection<PersonModel> persons = new ObservableCollection<PersonModel>();
            persons.Add(new PersonModel{ FirstName="Raj", LastName="kumar" });
            return persons;
        }
    }

用户控件是

<UserControl x:Class="WpfApplication1.UserControl1"
             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" 
             xmlns:local="clr-namespace:WpfApplication1"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <UserControl.DataContext>
        <local:PersonViewModel></local:PersonViewModel>
    </UserControl.DataContext>
    <Grid>      
        <Grid.RowDefinitions>
            <RowDefinition></RowDefinition>
            <RowDefinition></RowDefinition>
        </Grid.RowDefinitions>        
        <TextBlock Grid.Row="0" Text="FirstName"></TextBlock>
        <TextBox Text="{Binding FirstName,Mode=TwoWay}" Name="txtFirstName" Margin="115,0,0,106" DataContext="{Binding Path=Person}" Background="#FFF0F0F0" />
        <TextBlock Grid.Row="1" Text="FirstName"></TextBlock>
        <TextBox Grid.Row="1" Text="{Binding LastName,Mode=TwoWay}" Name="txtLastName" Margin="115,0,0,110" DataContext="{Binding Path=Person}" Background="#FFF0F0F0" />
    </Grid>
</UserControl>

我需要重新绑定没有发生的数据上下文

can you suggest what is the problem?
4

2 回答 2

0

ViewModel 没有实现 INotifyPropertyChanged,因此当您设置集合时,视图不会收到有关此更改的通知。

一般来说,我建议不要使用这种设置:您正在混合模型和视图模型,这将在模型的视图中创建依赖关系。虽然它现在可能在未来的版本中工作,但它可能会成为一个问题,而且它违反了 MVVM 模式。

于 2012-05-26T10:55:09.130 回答
0

更改此代码:

public class PersonViewModel : INotifyPropertyChanged
   {
       public ObservableCollection<PersonModel> _person = new ObservableCollection<PersonModel>();

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


       public PersonViewModel()
       {
           UpdateView = new DelegateCommand(this.OnUpdateView, this.CanUpdateView);

           var tmp = CookPersonData();
           Person.Clear();

           foreach (var item in tmp)
           {
               Person.Add(item);
           }
       }

       internal static ObservableCollection<PersonModel> CookPersonData()
       {
           ObservableCollection<PersonModel> persons = new ObservableCollection<PersonModel>();
           persons.Add(new PersonModel { FirstName = "Raj", LastName = "kumar" });
           return persons;
       }

       internal static ObservableCollection<PersonModel> CookPersonData2()
       {
           ObservableCollection<PersonModel> persons = new ObservableCollection<PersonModel>();
           persons.Add(new PersonModel { FirstName = "Raj2", LastName = "kumar2" });
           return persons;
       }

       public ICommand UpdateView { get; private set; } 
       private void OnUpdateView()
       {
           var tmp = CookPersonData2();
           Person.Clear();

           foreach (var item in tmp)
           {
               Person.Add(item);
           }
       }
       private bool CanUpdateView()
       {
           return true;
       }

       protected void RaisePropertyChanged(string propertyName)
       {
           var handler = PropertyChanged;
           if (handler != null)
           {
               handler(this, new PropertyChangedEventArgs(propertyName));
           }
       }

       public event PropertyChangedEventHandler PropertyChanged;
   }

和 XAML 文件:

   <Window.DataContext>
        <local:PersonViewModel />
    </Window.DataContext>
    <StackPanel>
        <Grid>
        <Grid.RowDefinitions>
            <RowDefinition></RowDefinition>
            <RowDefinition></RowDefinition>

        </Grid.RowDefinitions>
        <TextBlock Grid.Row="0" Text="FirstName"></TextBlock>
        <TextBox Text="{Binding Person[0].FirstName,Mode=TwoWay}" Name="txtFirstName" Margin="115,0,0,106"  Background="#FFF0F0F0" />
        <TextBlock Grid.Row="1" Text="FirstName"></TextBlock>
        <TextBox Grid.Row="1" Text="{Binding Person[0].LastName,Mode=TwoWay}" Name="txtLastName" Margin="115,0,0,110" Background="#FFF0F0F0" />


    </Grid>
        <Button Content="Update" Command="{Binding UpdateView}" />
    </StackPanel>
于 2012-05-26T10:58:21.657 回答