[英]Binding custom data structures to DataGrid ItemsSource in WPF
[英]WPF DataGrid Combobox binding to data on load with separate ItemsSource
我有一个我无法解决的难题,至少从以MVVM的角度来看这是没有道理的。
我有一个数据网格,其中包含客户-雇员数据以及每个客户/雇员关系的开始日期和结束日期。
DataGrid的所有ItemsSource是绑定到ClientToEmpObservableCollection的ClientToEmp CollectionViewSource。 但是,在允许他们更改/更新当前雇员的组合框列中,ItemsSource是绑定到User ObservableCollection的Employee CollectionViewSource(即,他们可以为此客户端选择的所有雇员的列表)。
这部分工作正常,当我单击组合框时,将显示适当的员工以从组合框中选择。 但是,当加载数据网格时,我希望将ClientToEmp cvs中的CurrentEmp显示为选定的员工(即,当前分配给此客户端的员工)。 当他们单击它时,他们应该能够更改Employee(来自单独的员工cvs),然后将更新ClientToEmp cvs中的值。
<DataGrid Name="ClientToEmpMDG" ItemsSource="{Binding cvsClientToEmp}"
AutoGenerateColumns="False" AutoGeneratingColumn="Gen_AutoGridColumns">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding ClientName}" Header="Client Name" IsReadOnly="True"/>
<DataGridTemplateColumn Header="Current Emp">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox ItemsSource="{Binding Path=DataContext.cvsEmp, RelativeSource={RelativeSource AncestorType=Window}}"
DisplayMemberPath="DisplayName"
SelectedValuePath="User_ID"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="Start Date">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<DatePicker Name="StartDateDP" SelectedDate="{Binding Path=Start_Date}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="End Date">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<DatePicker Name="EndDateDP" SelectedDate="{Binding Path=End_Date}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
我如何才能使当前员工在负载下正常显示? 所选值应与两个ObservableCollections中的User_ID相匹配。 如果我使用SelectedValue属性,则网格中的所有组合框都显示相同的值,并且它仍未正确绑定到我希望绑定到的cvsClientToEmp值。 我希望每个组合框都具有自己的价值,而不是所有共享一个单独的价值。
更新:我修复了所有组合框显示相同值的问题,方法是将“ IsSynchronizedWithCurrentItem”添加为False ...仍然无法将其绑定到数据库返回的值,即当前雇员是谁
您在ComboBox
缺少SelectedValue
<ComboBox ItemsSource="{Binding Path=DataContext.cvsEmp, RelativeSource={RelativeSource AncestorType=Window}}"
DisplayMemberPath="DisplayName"
SelectedValue="{Binding CurrentEmp}"
SelectedValuePath="User_ID"/>
另请参阅https://stackoverflow.com/a/4902454/10718884,以获得很好的解释
注意:还有一个DataGridComboBoxColumn
<DataGridComboBoxColumn
ItemsSource="{Binding Path=DataContext.cvsEmp, RelativeSource={RelativeSource AncestorType=Window}}"
DisplayMemberPath="DisplayName"
SelectedValueBinding="{Binding CurrentEmp}"
SelectedValuePath="User_ID">
</DataGridComboBoxColumn>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.