1

I posted this WPF DataGrid with DataGrid in RowDetailsTemplate earlier. Now I just want to figure out one part.

I have a DataGrid bound to a list of jobs. On each Job model is a list of Employees and a SelectedEmployee property.

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

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

Here's the grid 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>

When I click a row in the Employees grid, the SelectedEmployee property on the Job model does not fire. The Getter fires on startup, but the Setter doesn't fire when I select an employee

What am I doing wrong here?????

1
  • Did you find a solution for this? Commented Jul 19, 2020 at 0:09

2 Answers 2

5

Here's an example of how I found a way to do this:

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

in the code behind i had something like this:

var list = new List<OuterObject>()
{ 
    new OuterObject()
    {
         OuterObjectProperty = foo
         InnerCollection = new List<InnerObject>(){ Foo = bar}
    }
};
Sign up to request clarification or add additional context in comments.

2 Comments

In which class should we write the code behind? In the parent or nested child class?
@gnjago whichever object is currently binding to "OuterObject" is where this code should be
0

I think you need to set it TwoWay binding mode for SelectedItem.

SelectedItem={Binding SelectedEmployee, Mode=TwoWay}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.