简体   繁体   English

WPF DataGrid组合框通过单独的ItemsSource绑定到加载的数据

[英]WPF DataGrid Combobox binding to data on load with separate ItemsSource

I have a little bit of a quandary that I haven't been able to resolve, at least not in a method that makes sense from an MVVM standpoint. 我有一个我无法解决的难题,至少从以MVVM的角度来看这是没有道理的。

I have a datagrid that contains client-employee data with start and end dates for each client/employee relationship. 我有一个数据网格,其中包含客户-雇员数据以及每个客户/雇员关系的开始日期和结束日期。

The DataGrid overall ItemsSource is the ClientToEmp CollectionViewSource bound to the ClientToEmpObservableCollection. DataGrid的所有ItemsSource是绑定到ClientToEmpObservableCollection的ClientToEmp CollectionViewSource。 However, in the combobox column that allows them to change/update the current employee the ItemsSource is the Employee CollectionViewSource bound to the User ObservableCollection(ie, a list of all employees they can choose for this client). 但是,在允许他们更改/更新当前雇员的组合框列中,ItemsSource是绑定到User ObservableCollection的Employee CollectionViewSource(即,他们可以为此客户端选择的所有雇员的列表)。

This part works fine, when I click the combobox, the proper employees are displayed to select from in the combobox. 这部分工作正常,当我单击组合框时,将显示适当的员工以从组合框中选择。 However when the datagrid loads, I want the CurrentEmp from the ClientToEmp cvs to be showing as the selected employee(ie, the employee that is currently assigned to this client). 但是,当加载数据网格时,我希望将ClientToEmp cvs中的CurrentEmp显示为选定的员工(即,当前分配给此客户端的员工)。 When they click on it, they should be able to change the Employee(from the separate employee cvs), which would then update the value in the ClientToEmp cvs. 当他们单击它时,他们应该能够更改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>

How can I get the current employee to show properly on load? 我如何才能使当前员工在负载下正常显示? The selected value should match the User_ID from the two ObservableCollections. 所选值应与两个ObservableCollections中的User_ID相匹配。 If I use the SelectedValue Property, all comboboxes in the grid show the same value, and it still doesn't bind properly to the cvsClientToEmp value I want it to bind to. 如果我使用SelectedValue属性,则网格中的所有组合框都显示相同的值,并且它仍未正确绑定到我希望绑定到的cvsClientToEmp值。 I want each combobox to have it's own value, not all of them to share a single value. 我希望每个组合框都具有自己的价值,而不是所有共享一个单独的价值。

UPDATE: I fixed the issue with all of the comboboxes displaying the same value by changing adding "IsSynchronizedWithCurrentItem" to False...Still cannot get it to bind to the value coming back from the DB as to who the current employee is 更新:我修复了所有组合框显示相同值的问题,方法是将“ IsSynchronizedWithCurrentItem”添加为False ...仍然无法将其绑定到数据库返回的值,即当前雇员是谁

You are missing SelectedValue in your ComboBox 您在ComboBox缺少SelectedValue

<ComboBox ItemsSource="{Binding Path=DataContext.cvsEmp, RelativeSource={RelativeSource AncestorType=Window}}"
    DisplayMemberPath="DisplayName"
    SelectedValue="{Binding CurrentEmp}"
    SelectedValuePath="User_ID"/>

See also https://stackoverflow.com/a/4902454/10718884 for a good explanation 另请参阅https://stackoverflow.com/a/4902454/10718884,以获得很好的解释


Note: There is also a DataGridComboBoxColumn 注意:还有一个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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM