0

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

MattE
  • 1,044
  • 1
  • 14
  • 34
  • I am having a little trouble following along. Could you possibly add a picture of what you mean? – Selthien Jan 09 '19 at 21:03
  • @Selthien I can't add a picture but basically I have a DataGrid where I am loading data from the server into an ObservableCollection populating Client To Employee relationships. User_ID, CurrentEmp, ClientName, StartDate, EndDate are the fields. The column "Current Emp" is a combobox column, with combobox items of all employees(the items source is different from the DataGrid). I want each row to automatically select the CurrentEmp in the ClientToEmp ItemsSource the datagrid is using as the selected value(ie, display this in the combobox when it loads). I cannot get this working. – MattE Jan 09 '19 at 21:09
  • @MattE Of which type is "CurrentEmp"? Same as "User_ID" of an item from your ComboBox-Source? – nosale Jan 09 '19 at 21:27

1 Answers1

0

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>
nosale
  • 808
  • 6
  • 14
  • Doesn't work...already tried that...the problem is I need to get it to bind to the cvsClientToEmp User_ID value of the datagrid row NOT the cvsEmp ItemsSource that the combobox is bound to...it also will change every combobox in the datagrid to have this value instead of only the single one I change. Also If I use the DataGridComboBoxColumn I would need to edit the ElementStyle of the ComboBox and EditedElementStyle...your solution is not valid for that – MattE Jan 09 '19 at 20:37
  • @MattE Okay that is strange, cause `SelectedValue` corresponds to the item of the row. – nosale Jan 09 '19 at 21:11
  • I even tried SelectedValue="{Binding Source=cvsClientToEmp, Path=User_ID}" and still the same issue... – MattE Jan 09 '19 at 21:13
  • @MattE Edit my answer based on your comment about the properties. This should work as long the type of `CurrentEmp`(item in row) can be cast to the type of `User_ID`(item in ComboBox) – nosale Jan 09 '19 at 21:36
  • Need it to be like this, can't get it. – MattE Jan 09 '19 at 21:44
  • @MattE Am I right? You have a collection `ClientToEmp` which lets say contains items of type `ClientToEmpRelation` with the properties User_ID, CurrentEmp, ClientName, StartDate, EndDate. `ClientToEmp` is the source of your `DataGrid`. Then you have a second collection `Employes` which contains items of type `Employe` with at least the properties User_Id and DisplayName. Now lets say you change the value of the ComboBox in the row which contains the `ClientToEmpRelation` with name/id = 1337. Now what exactly should happen? – nosale Jan 09 '19 at 22:13
  • Try to keep DisplayMemberPath & SelectedValuePath same. – Smits Jan 10 '19 at 04:37
  • @Smits There is no reason in doing so. In real life applications most of the time they have to differ cause of usability/readability. – nosale Jan 10 '19 at 08:04