2016-04-29 15 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を押してEnterキーを押します。私はデータが検索され、人口が集まっているとは思わない。助けてください。

+0

こんにちは - 私の謝罪を受け入れてください。 Empclsのコードがコードfileに表示されなかった理由がわかりません。私はこの種の間違いが起こった投稿を作成します。このフォーラムのウィンドウで、コードを正しく追加するにはどうすればいいのか教えてください。ありがとう – Mohan

+0

コントロール+ kまたはコマンド+ kを押すだけでハイライトされます。余分な空白行のいくつかを削除することも考慮する必要があります。 – Laurel

+0

ありがとう、次回私はそれを行うでしょう – Mohan

答えて

1

あなたのバインディングはTextBoxtxtEmpId1で間違っています。 ElementNameは、要素を名前で参照するために使用されます(たとえば、txtEmpId1が要素名です)。

DataContextSearchEmpVMのインスタンスに設定するので、そのプロパティを直接参照することができます。あなたはまた、単に注意することが

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

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

を変更する場合は、それはできるだけ早くあなたが値を変更するようにフィールドをロードUpdateSourceTrigger=PropertyChangedを使用しているように、それは、動作します。あなたはそれを省略して、デフォルト(これはUpdateSourceTrigger=LostFocusと同じです)を使用して、TextBoxのタブからデータをロードすることができます。異なるUpdateSourceTriggerの値のリストをMSDNに取得することができます。

+0

おかげで、あなたの助けが問題を解決した、それは完璧に働いた! – Mohan

+0

よろしくお願いします。問題ない。 – Tone

+0

@Mohan、これはあなたの問題を解決した答えとしてマークする必要があります。ありがとう。 – Joe

関連する問題