DataGrid.RowDetailsTemplate中的WPF DataGrid

时间:2013-09-23 23:11:00

标签: wpf datagrid

我正在尝试在WPF DataGrid的RowDetailsTemplate中创建一个DataGrid。

我有一系列乔布斯。每个Job可以分配一个或多个Employees:

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

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

当我运行它并单击一行时,我得到:

在使用ItemsSource时,操作无效。使用ItemsControl.ItemsSource访问和修改元素

如果删除内部网格的列定义,则不会出错。但随后它会自行生成列。我需要自己指定列。

这里有什么问题?

由于

1 个答案:

答案 0 :(得分:16)

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

<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将其绑定到另一个源,这是错误的来源。