【问题标题】:WPF DataGrid inside DataGrid.RowDetailsTemplateDataGrid.RowDetailsTemplate 内的 WPF DataGrid
【发布时间】:2013-09-29 00:09:36
【问题描述】:

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

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

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 访问和修改元素

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

这里有什么问题?

【问题讨论】:

    标签: wpf datagrid


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2011-02-28
      • 2014-08-30
      • 1970-01-01
      • 2014-10-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-28
      相关资源
      最近更新 更多