WPF嵌套DataGrid绑定不起作用

时间:2013-09-24 21:31:10

标签: wpf binding datagrid

我之前发布了此WPF DataGrid with DataGrid in RowDetailsTemplate。现在我只想弄清楚一个部分。

我有一个绑定到作业列表的DataGrid。在每个作业模型上都是Employees和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>

当我单击Employees网格中的一行时,Job模型上的SelectedEmployee属性不会触发。 Getter在启动时触发,但是当我选择员工时,Setter不会触发

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

2 个答案:

答案 0 :(得分: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}
    }
};

答案 1 :(得分:0)

我认为你需要为SelectedItem设置它的TwoWay绑定模式。

SelectedItem = {Binding SelectedEmployee,Mode = TwoWay}