DataGrid.RowDetailsTemplate 中的 WPF DataGrid

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18970386/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 09:42:54  来源:igfitidea点击:

WPF DataGrid inside DataGrid.RowDetailsTemplate

wpfdatagrid

提问by CoderForHire

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

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

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

我有一个工作集。每个 Job 都可以分配一个或多个员工:

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:

和我的 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

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

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?

这里有什么问题?

回答by Shoe

You need to define your inner datagrid's columns correctly

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

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

Turn this to

把这个变成

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

Essentially setting it the way you are is attempting to set the source in the xaml and then bind it to another source through ItemsSourcewhich is where the error is coming from.

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