1

我之前在 RowDetailsTemplate 中发布了这个带有 DataGrid 的 WPF DataGrid。现在我只想弄清楚一部分。

我有一个绑定到作业列表的 DataGrid。每个 Job 模型上都有一个员工列表和一个 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 属性不会触发。Getter 在启动时触发,但当我选择员工时 Setter 不会触发

我在这里做错了什么??????

4

2 回答 2

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}
    }
};
于 2013-11-14T13:46:22.617 回答
0

我认为您需要为 SelectedItem 设置双向绑定模式。

SelectedItem={绑定 SelectedEmployee, Mode=TwoWay}

于 2014-03-04T23:36:27.800 回答