简体   繁体   中英

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.

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

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

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

You are missing SelectedValue in your ComboBox

<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


Note: There is also a DataGridComboBoxColumn

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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