2013-09-24 153 views
1

我張貼這種WPF DataGrid with DataGrid in RowDetailsTemplate更早。現在我只想弄清楚一個部分。WPF嵌套的DataGrid綁定不工作

我有必然的作業列表一個DataGrid。在每個Job模型上都有一個Employees和SelectedEmployee屬性的列表。

public class Job : _Base 
{ 
    private string _JobName = string.Empty; 
    public string JobName 
    { 
     get { return _JobName; } 
     set 
     { 
      if (_JobName != value) 
      { 
       _JobName = value; 
       RaisePropertyChanged("JobName"); 
      } 
     } 
    } 

    private string _JobNumber = string.Empty; 
    public string JobNumber 
    { 
     get { return _JobNumber; } 
     set 
     { 
      if (_JobNumber != value) 
      { 
       _JobNumber = value; 
       RaisePropertyChanged("JobNumber"); 
      } 
     } 
    } 

    private ObservableCollection<Employee> _Employees; 
    public ObservableCollection<Employee> Employees 
    { 
     get { return _Employees; } 
     set 
     { 
      if (_Employees != value) 
      { 
       if (_Employees != value) 
       { 
        _Employees = value; 
        RaisePropertyChanged("Employees"); 
       } 
      } 
     } 
    } 

    private Employee _SelectedEmployee; 
    public Employee SelectedEmployee 
    { 
     get { return _SelectedEmployee; } 
     set 
     { 
      if (_SelectedEmployee != value) 
      { 
       _SelectedEmployee = value; 
       RaisePropertyChanged("SelectedEmployee"); 
      } 
     } 
    } 

    public Job() 
    { 
     Employees = new ObservableCollection<Employee>(); 
    } 
} 

public class Employee : _Base 
{ 
    private string _EmployeeName = string.Empty; 
    public string EmployeeName 
    { 
     get { return _EmployeeName; } 
     set 
     { 
      if (_EmployeeName != value) 
      { 
       _EmployeeName = value; 
       RaisePropertyChanged("EmployeeName"); 
      } 
     } 
    } 

    private bool _IsChecked = false; 
    public bool IsChecked 
    { 
     get { return _IsChecked; } 
     set 
     { 
      if (_IsChecked != value) 
      { 
       _IsChecked = value; 
       RaisePropertyChanged("IsChecked"); 
      } 
     } 
    } 
}   

這裏的電網XAML:

<DataGrid ItemsSource="{Binding Jobs}" 
      SelectedItem="{Binding SelectedJob}" 
      AutoGenerateColumns="False"> 

    <DataGrid.Columns> 
     <DataGridTextColumn Header="Job Name" Binding="{Binding JobName}" /> 
     <DataGridTextColumn Header="Job Number" Binding="{Binding JobNumber}" /> 
    </DataGrid.Columns> 

    <DataGrid.RowDetailsTemplate> 
     <DataTemplate> 

      <StackPanel Orientation="Vertical"> 

       <DataGrid ItemsSource="{Binding Employees}" 
          SelectedItem="{Binding SelectedEmployee}" 
          AutoGenerateColumns="False"> 

        <DataGrid.Columns> 
         <DataGridCheckBoxColumn Binding="{Binding IsChecked}"/> 
         <DataGridTextColumn Binding="{Binding EmployeeName}"/> 
        </DataGrid.Columns> 

       </DataGrid> 

       <Button Margin="5" 
         Height="23" 
         Width="75" 
         HorizontalAlignment="Left" 
         Content="Remove"/> 

      </StackPanel> 


     </DataTemplate> 
    </DataGrid.RowDetailsTemplate> 

</DataGrid> 

當我點擊僱員格一行,在工作模式SelectedEmployee財產不火。吸氣觸發啓動時,但是當我選擇一個員工

什麼我錯在這裏做二傳手不火?????

+0

你有沒有找到一個解決這個? – DLeh

回答

5

這裏是我如何找到一個辦法做到這一點的例子:

<DataGrid x:Name="dgOuter" ItemsSource="{Binding}"> 
    <DataGrid.Columns> 
     <DataGridTextColumn Header="OuterObjectProperty" Binding="{Binding OuterObjectProperty}"/> 
    </DataGrid.Columns> 
    <DataGrid.RowDetailsTemplate> 
     <DataTemplate> 
      <DataGrid x:Name="dgInner" ItemsSource="{Binding InnerCollection}"> 
       <DataGrid.Columns> 
        <DataGridTextColumn Header="Property" Binding="{Binding Property}"/> 
       </DataGrid.Columns> 
      </DataGrid> 
     </DataTemplate> 
    </DataGrid.RowDetailsTemplate> 
</DataGrid> 

在後面的代碼我有這樣的事情:

var list = new List<OuterObject>() 
{ 
    new OuterObject() 
    { 
     OuterObjectProperty = foo 
     InnerCollection = new List<InnerObject>(){ Foo = bar} 
    } 
}; 
+0

這是一個真棒建議。謝謝。 –

0

我認爲你需要設置雙向綁定爲SelectedItem模式。

的SelectedItem = {結合SelectedEmployee,模式=雙向}