5

I am trying to create a DataGrid inside a WPF DataGrid's RowDetailsTemplate.

I have a collection of Jobs. Each Job can have one or more Employees assigned to it:

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");
                }
            }
        }
    }

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

and

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");
            }
        }
    }
}     

and my XAML:

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

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

    <DataGrid.RowDetailsTemplate>
        <DataTemplate>
            <DataGrid ItemsSource="{Binding Employees}"
                        AutoGenerateColumns="False">
                <DataGridCheckBoxColumn Binding="{Binding IsChecked}"/>
                <DataGridTextColumn Binding="{Binding EmployeeName}"/>
            </DataGrid>
        </DataTemplate>
    </DataGrid.RowDetailsTemplate>

</DataGrid>

When I run this and click a row, I get:

Operation is not valid while ItemsSource is in use. Access and modify elements with ItemsControl.ItemsSource instead

If I remove the inner grid's column definitions, then it doesn't error. But then it's generating the columns by itself. I need to specify the columns myself.

What is wrong here?

4

1 回答 1

18

您需要正确定义内部数据网格的列

<DataGrid ItemsSource="{Binding Employees}" AutoGenerateColumns="False">
    <DataGridCheckBoxColumn Binding="{Binding IsChecked}"/>
    <DataGridTextColumn Binding="{Binding EmployeeName}"/>
</DataGrid>

把这个变成

<DataGrid ItemsSource="{Binding Employees}" AutoGenerateColumns="False">
    <DataGrid.Columns>
       <DataGridCheckBoxColumn Binding="{Binding IsChecked}"/>
       <DataGridTextColumn Binding="{Binding EmployeeName}"/>
    </DataGrid.Columns>
</DataGrid>

基本上设置它的方式是尝试在 xaml 中设置源,然后将其绑定到另一个源,通过ItemsSource该源是错误的来源。

于 2013-09-23T23:53:50.987 回答