[英]WPF Nested DataGrid Binding Not Working
I posted this WPF DataGrid with DataGrid in RowDetailsTemplate earlier. 我之前在RowDetailsTemplate中将此WPF DataGrid和DataGrid一起发布了。 Now I just want to figure out one part. 现在我只想弄清楚一部分。
I have a DataGrid bound to a list of jobs. 我有一个绑定到作业列表的DataGrid。 On each Job model is a list of Employees and a SelectedEmployee property. 在每个Job模型上都有一个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>();
}
}
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: 这是表格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. 当我单击“雇员”网格中的一行时,“作业”模型上的SelectedEmployee属性不会触发。 The Getter fires on startup, but the Setter doesn't fire when I select an employee Getter在启动时触发,但是当我选择员工时,Setter不会触发
What am I doing wrong here????? 我在这里做错了什么?
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}
}
};
I think you need to set it TwoWay binding mode for SelectedItem. 我认为您需要为SelectedItem设置TwoWay绑定模式。
SelectedItem={Binding SelectedEmployee, Mode=TwoWay} SelectedItem = {绑定SelectedEmployee,Mode = TwoWay}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.