2016-06-26 17 views
0

まずは新しくWPFの学習を始めました。WPFのエキスパンダーでlistviewを使用するとバインディングが機能しない

以下に示すように、私は、従業員クラスを持っている:

public class Employee : INotifyPropertyChanged 
    { 
     public Employee() 
     { 
      _subEmployee = new ObservableCollection<Employee>(); 
     } 

     private string _name; 

     public ObservableCollection<Employee> SubEmployee 
     { 
      get { return _subEmployee; } 
      set 
      { 
       _subEmployee = value; 
       NotifiyPropertyChanged("SubEmployee"); 
      } 
     } 

     public string Name 
     { 
      get { return _name; } 
      set 
      { 
       _name = value; 
       NotifiyPropertyChanged("Name"); 
      } 
     } 

     ObservableCollection<Employee> _subEmployee; 

     public event PropertyChangedEventHandler PropertyChanged; 
     void NotifiyPropertyChanged(string property) 
     { 
      if (PropertyChanged != null) 
       PropertyChanged(this, new PropertyChangedEventArgs(property)); 
     } 
    } 

私はメインウィンドウクラスで従業員の監視可能なコレクションを作成し、以下に示すように、同じコレクションにDataContextのを設定している:

public partial class MainWindow : Window 
    { 
     public ObservableCollection<Employee> Emp { get; private set; } 

     public MainWindow() 
     { 
      InitializeComponent(); 
      Emp = new ObservableCollection<Employee>(); 
      Emp.Add(new Employee(){Name = "Anuj"}); 
      Emp.Add(new Employee() { Name = "Deepak" }); 
      Emp.Add(new Employee() { Name = "Aarti" }); 

      Emp[0].SubEmployee.Add(new Employee(){Name = "Tonu"}); 
      Emp[0].SubEmployee.Add(new Employee() { Name = "Monu" }); 
      Emp[0].SubEmployee.Add(new Employee() { Name = "Sonu" }); 

      Emp[2].SubEmployee.Add(new Employee() { Name = "Harsh" }); 
      Emp[2].SubEmployee.Add(new Employee() { Name = "Rahul" }); 
      Emp[2].SubEmployee.Add(new Employee() { Name = "Sachin" }); 
      this.DataContext = Emp; 
     } 
    } 

さて、xaml.csでは以下のコードを書いています:

<Grid> 
     <ListView ItemsSource="{Binding Emp}"> 
      <ListView.ItemTemplate> 
       <DataTemplate DataType="{x:Type vm:Employee }"> 
        <Expander Header="{Binding Name}"> 
         <ListView ItemsSource="{Binding SubEmployee}"> 
          <ListView.ItemTemplate> 
           <DataTemplate> 
            <TextBlock Text ="{Binding Name}"/> 
           </DataTemplate> 
          </ListView.ItemTemplate> 
         </ListView> 
        </Expander> 
       </DataTemplate> 
      </ListView.ItemTemplate> > 
     </ListView> 
    </Grid> 

今私はエキスパンダーボタンでEmployeeオブジェクトのコレクションを取得する必要があります。ボタンをクリックすると、すべてのSubEmployeeが表示されます。しかし、残念ながら私は期待された結果を得ていません。

バインディングの問題を解決するのを手伝ってください。あなたのDataContext

答えて

0

は、直接、現在のDataContextItemsSourceをバインドする必要がありますLitViewので、外ObservableCollection<Employee>です。つまり、DataContextにはEmpというプロパティがありません。あなたの状況はEmpプロパティ

<ListView ItemsSource="{Binding}"> 
    <ListView.ItemTemplate> 
     <!-- .... --> 
    </ListView.ItemTemplate> 
</ListView> 

かであるとしてXAMLを残しているし、このコピー/貼り付けエラーが、約あなたから苦情だったDataContext EDIT

this.DataContext = this; 

思想を設定する方法を変更しました無効な操作例外エラー秒のため>の末尾にItemTemplateタグ

</ListView.ItemTemplate> > 
+0

私はdataContextをthis.DataContext = thisに変更しました。コードを実行しているときに、コールスタックに「無効な操作例外が処理されませんでした」という外部コードのみが含まれているというエラーが表示されます。 –

0

あなたのコードをテストしました。 DataContextが正しく設定されていません。 私はまた、テキストに色を付けました。 これを試してください。私の最後で完璧に働いています!

XAML:

<Grid> 
    <ListView ItemsSource="{Binding Emp}"> 
     <ListView.ItemTemplate> 
      <DataTemplate DataType="{x:Type vm:Employee }"> 
       <Expander Header="{Binding Name}"> 
        <ListView ItemsSource="{Binding SubEmployee}"> 
         <ListView.ItemTemplate> 
          <DataTemplate> 
           <TextBlock Foreground="Black" Text ="{Binding Name}"/> 
          </DataTemplate> 
         </ListView.ItemTemplate> 
        </ListView> 
       </Expander> 
      </DataTemplate> 
     </ListView.ItemTemplate> 
    </ListView> 
</Grid> 

コードの後ろ:

public partial class MainWindow : Window 
{ 
    public ObservableCollection<Employee> Emp { get; set; } 

    public MainWindow() 
    { 
     InitializeComponent(); 
     DataContext = this; 

     Emp = new ObservableCollection<Employee>(); 
     Emp.Add(new Employee() { Name = "Anuj" }); 
     Emp.Add(new Employee() { Name = "Deepak" }); 
     Emp.Add(new Employee() { Name = "Aarti" }); 

     Emp[0].SubEmployee.Add(new Employee() { Name = "Tonu" }); 
     Emp[0].SubEmployee.Add(new Employee() { Name = "Monu" }); 
     Emp[0].SubEmployee.Add(new Employee() { Name = "Sonu" }); 

     Emp[2].SubEmployee.Add(new Employee() { Name = "Harsh" }); 
     Emp[2].SubEmployee.Add(new Employee() { Name = "Rahul" }); 
     Emp[2].SubEmployee.Add(new Employee() { Name = "Sachin" }); 

    } 
} 

他のデータクラスは同じまま!

+0

私はdataContextをthis.DataContext = thisで変更しました。コードを実行しているときに、コールスタックに「無効な操作例外が処理されませんでした」という外部コードのみが含まれているというエラーが表示されます。 –

+1

さらにいくつかの変更があります。慎重に読む。投稿したコード全体を置き換え、それが機能していない場合は元に戻します。 – ViVi

+0

なぜあなたは**正解**のように働いていない答えを選択しましたか?私はあなたのコードをテストしてその動作を保証しました。 – ViVi

関連する問題