0
votes

I have a DataGrid with a combobox assigned of Customer IDs, Each Customer ID may have many Account IDs which are assigned in their own combo box. All I want to do is, when a user selects a Customer ID from the combobox, the Account IDs are then updated with the specific CustomerID that was chosen.

I think I am nearly there with the code.

Here I have the itemsource of the customerId applied inside the combobox elementstyle. As you can see I have a property of SelectedItem when an Item is selected from the customer box.

                <DataGridComboBoxColumn.ElementStyle>
                    <Style TargetType="ComboBox">
                        <Setter Property="ItemsSource"
                Value="{Binding DataContext.EntityCollection,
                          RelativeSource={RelativeSource Mode=FindAncestor, 
                                                     AncestorType=Window}}"/>
                        <Setter Property="DisplayMemberPath" Value="Name"/>
                        <Setter Property="SelectedItem" Value="{Binding SelectedItem}"></Setter>
                        <Setter Property="HorizontalAlignment" Value="Center"></Setter>
                    </Style>
                </DataGridComboBoxColumn.ElementStyle>

So inside the Combobox of the Account Id, I am not sure where to put the SelectedItem.AID code...

I have put it inside the SelectedValueBinding property in the first line, but it doesnt work. do I need to put SelectedItem inside the Itemsource porperty inside the EditingElementStyle tag?

            <DataGridComboBoxColumn SelectedValueBinding="{Binding SelectedItem.AID}"                SelectedValuePath="SID"  Header="SID" Width="70">
                <DataGridComboBoxColumn.EditingElementStyle>
                    <Style TargetType="ComboBox">
                        <Setter Property="ItemsSource"
                Value="{Binding DataContext.EntityCollection, 
                          RelativeSource={RelativeSource Mode=FindAncestor, 
                                                    AncestorType=Window}}"/>
                        <Setter Property="DisplayMemberPath" Value="AID"/>
                    </Style>
                </DataGridComboBoxColumn.EditingElementStyle>
                <DataGridComboBoxColumn.ElementStyle>
                    <Style TargetType="ComboBox">
                        <Setter Property="ItemsSource"
                Value="{Binding DataContext.EntityCollection,
                          RelativeSource={RelativeSource Mode=FindAncestor, 
                                                     AncestorType=Window}}"/>
                        <Setter Property="DisplayMemberPath" Value="AID"/>
                        <Setter Property="HorizontalAlignment" Value="Center"></Setter>
                    </Style>
                </DataGridComboBoxColumn.ElementStyle> 

However I could be way off, so any help I would be grateful.

Thanks

1

1 Answers

0
votes
<Setter Property="ItemsSource" Value="{Binding DataContext.EntityCollection,

I'm not entirely sure if that is possible, though I would not recommend using that this way.

Bind your ViewModel in the code behind to the DataContext of your View. Now you can bind the ItemsSource to any ObservableCollection Property in your ViewModel. That'd probably be your EntityCollection

Well, when using a DataGridComboBoxColumn, all you need to do, is to tell this column where the data is located by binding the target property.

If you'd have a list of Customer applied to your DataGrid and your property is named ID, your markup might be as follows:

SelectedValueBinding="{Binding ID, Mode=TwoWay}"

Checking the Mode is highly required in order to transmit changes made to your property.

Now, to apply any changes to your other ComboBox, e.g. adding or creating items, you need to use another ObservableCollection and bind it to that second Combobox. If you done that, you can control the items of the second ComboBox when any changes are made to the first one.

Just a short sample how this might look:

private uint _ID;
public uint ID
{
    get { return _ID; }
    set
    {
        if (_ID == value) return;
        _ID = value;
        NotifyOfPropertyChange("ID");
        // Do something with your second ComboBox.
        // You could create another ObservableCollection and bind that to your second                   ComboBox and add some items representing the Account IDs
    }
}

Don't forget to notify your View when making changes.

If that wasn't the answer you are looking for, please be more specific. Your actual question was very hard to read, I only understood the first paragraph and explained, how you can archive what you want to do.