2016-04-29 51 views
0

嗨 - 我是WPF MVVM的新手。我正在尝试下面的代码。基本MVVM WPF不填充数据

MainWindow.xaml:

<Window x:Class="BasicMVVMWPF.MainWindow" 
    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"  
     xmlns:vm="clr-namespace:BasicMVVMWPF.ViewModel" 
    Title="MainWindow" Height="350" Width="525"> 
<Window.DataContext> 
    <vm:SearchEmpVM /> 
</Window.DataContext> 
<Grid> 

    <Grid.RowDefinitions> 
     <RowDefinition Height="auto" ></RowDefinition> 
     <RowDefinition Height="auto"></RowDefinition> 
     <RowDefinition Height="auto"></RowDefinition> 
     <RowDefinition Height="auto" ></RowDefinition> 
    </Grid.RowDefinitions> 


    <StackPanel> 
     <Grid Margin="0,51,0,-48" Grid.RowSpan="4"> 
      <Grid.RowDefinitions> 
       <RowDefinition Height="auto"></RowDefinition> 
       <RowDefinition Height="auto"></RowDefinition> 
      </Grid.RowDefinitions> 
      <Grid.ColumnDefinitions> 
       <ColumnDefinition Width="auto"/> 
       <ColumnDefinition Width="auto"/> 
      </Grid.ColumnDefinitions> 
      <Label Grid.Row="0" Grid.Column="0" Content="EmpId:"/> 
      <TextBox x:Name="txtEmpId1" Text="{Binding ElementName=Window,Path=DataContext.EmpId,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Grid.Row="0" Grid.Column="1" ></TextBox> 
      <StackPanel DataContext="{Binding SearchCls}" Grid.Column="1" Grid.Row="1"> 
       <GroupBox> 
        <GroupBox.HeaderTemplate> 
         <DataTemplate> 
          <Label Content="Employee Information"/> 

         </DataTemplate> 
        </GroupBox.HeaderTemplate> 
        <Grid > 
         <Grid.RowDefinitions> 
          <RowDefinition Height="26*"/> 
          <RowDefinition Height="26*"/> 
          <RowDefinition Height="26*"/> 
          <RowDefinition Height="26*"/> 
          <RowDefinition Height="26*"/> 
         </Grid.RowDefinitions> 
         <Grid.ColumnDefinitions> 
          <ColumnDefinition Width="Auto"/> 
          <ColumnDefinition Width="Auto"/> 
          <ColumnDefinition Width="Auto"/> 
          <ColumnDefinition Width="Auto"/> 
          <ColumnDefinition Width="Auto"/> 
         </Grid.ColumnDefinitions> 
         <Label Grid.Row="0" Grid.Column="0" Content="Name:" Grid.ColumnSpan="3" /> 
         <TextBox Text="{Binding Name}" Grid.Row="0" Grid.Column="3" Width="174"/> 
         <Label Grid.Row="1" Grid.Column="0" Content="Designation:" Grid.ColumnSpan="3"/> 
         <TextBox Text="{Binding Designation}" Grid.Row="1" Grid.Column="3" Width="174"/> 

         <Label Grid.Row="2" Grid.Column="0" Content="Department:" Grid.ColumnSpan="3" /> 
         <TextBox Text="{Binding Department}" Grid.Row="2" Grid.Column="3" Width="174"/> 

        </Grid> 



       </GroupBox> 
      </StackPanel> 

     </Grid> 

    </StackPanel> 


</Grid> 

EmpCls.cs

namespace BasicMVVMWPF.Entity 
{ 
    class EmpCls 
    { 
     private int _empNo; 
     public int EmpNo 
     { 
      get 
      { 
       return _empNo; 
      } 
      set 
      { 
       _empNo = value; 
      } 
     } 
     private string _name; 
     public string Name 
     { 
      get 
      { 
       return _name; 
      } 
      set 
      { 
       _name = value; 
      } 
     } 
     private string _designation; 
     public string Designation 
     { 
      get 
      { 
       return _designation; 
      } 
      set 
      { 
       _designation = value; 
      } 
     } 
     private string _department; 
     public string Department 
     { 
      get 
      { 
       return _department; 
      } 
      set 
      { 
       _department = value; 
      } 
     } 
    } 
} 

SearchEmpVM.cs

namespace BasicMVVMWPF.ViewModel 
{ 
    class SearchEmpVM : INotifyPropertyChanged 
    { 
     List<EmpCls> EmpList = new List<EmpCls>(); 

     public SearchEmpVM() 
     { 
      // Add sample employee details into employee list  
      EmpList.Clear(); 
      EmpList.Add(new EmpCls { EmpNo = 1, Name = "John", Department = "IT", Designation = "Developer" }); 
      EmpList.Add(new EmpCls { EmpNo = 2, Name = "Mark", Department = "IT", Designation = "Tester" }); 
      EmpList.Add(new EmpCls { EmpNo = 3, Name = "Robert", Department = "IT", Designation = "DB Developer" }); 

     } 
     #region properties 

     private EmpCls _searchcls = new EmpCls(); 
     public EmpCls SearchCls 
     { 
      get { return _searchcls; } 

      set 
      { 
       _searchcls = value; 

       RaisePropertyChanged("SearchCls"); 

      } 
     } 

     private int _empid; 
     public int EmpId 
     { 
      get 
      { 
       return _empid; 

      } 

      set 
      { 
       _empid = value; 

       RaisePropertyChanged("EmpId"); 
       PopulteEmpDetails(_empid); 
      } 


     } 


     #endregion 

     private void PopulteEmpDetails(int _empid) 
     { 

      SearchCls = EmpList.Where(x => x.EmpNo == _empid).Single(); 

     } 


     #region INotifyPropertyChanged 

     public event PropertyChangedEventHandler PropertyChanged; 
     public void RaisePropertyChanged(string propertyName) 
     { 
      if (PropertyChanged != null) 
      { 
       PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
      } 
     } 
     #endregion  
    } 
} 

当第一个文本框执行的项目,我已经进入1并按下确认键。我没有看到数据被搜索和填充。请帮忙。

+0

嗨 - 请接受我的道歉。我不知道为什么Empcls代码不会显示在代码文件中。每当我创建一个帖子时,就会发生这种错误。请让我知道如何在本论坛的窗口中正确添加代码。谢谢 – Mohan

+0

只要按下控制键+ k或命令+ k,使其突出显示。您应该考虑删除一些多余的空白行。 – Laurel

+0

谢谢,下次我会做 – Mohan

回答

1

您对TextBoxtxtEmpId1的绑定是错误的。 ElementName用于按名称引用元素(例如txtEmpId1是元素名称)。

您正在将DataContext设置为SearchEmpVM的实例,因此您可以直接引用它的属性。它的工作原理,如果你改变

Text="{Binding ElementName=Window,Path=DataContext.EmpId,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" 

Text="{Binding EmpId, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" 

也只是要注意,因为你正在使用UpdateSourceTrigger=PropertyChanged它,只要你改变数值加载领域。您可以选择忽略它并使用默认值(这相当于此属性的UpdateSourceTrigger=LostFocus),并且当您从TextBox中删除时,它将加载数据。您可以在MSDN处获得不同UpdateSourceTrigger值的列表。

+0

谢谢,你的帮助解决了这个问题,工作完美! – Mohan

+0

好听。没问题。 – Tone

+0

@莫汉,你应该把这个标记为答案,因为它解决了你的问题。谢谢。 – Joe