Pergunta

::EDIT note::

I have completely rewritten this question and it's title for simplification (my entity set names and properties were much more difficult to follow than this analogous scenario).

::/EDIT note::

I have really been struggling to find the "missing navigational link" between what I am able to see and save back to my entity Context from a DataGridComboBoxColumn.

MY ENTITY SETS:

InstructorClass

My VIEW: A DataGrid bound to a "Classes" Entity Set. It has two columns:
- A DataGridComboBoxColumn (which displays InstructorNames for each class)
- A DataGridTextColumn (which diplays classes by Name)

<DataGrid x:Name="DataGrid1" DockPanel.Dock="Top" Background="Transparent" 
              ItemsSource="{Binding ClassesObservableCollection, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
              AutoGenerateColumns="False" >
        <DataGrid.Columns>
            <DataGridComboBoxColumn Width="220" 
                                    Header="Instructor" 
                                    SelectedValueBinding="{Binding InstructorID, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" 
                                    SelectedValuePath="InstructorID"
                                    DisplayMemberPath="InstructorName">
                <DataGridComboBoxColumn.EditingElementStyle>
                    <Style TargetType="ComboBox">
                        <Setter Property="ItemsSource" Value="{Binding RelativeSource={RelativeSource AncestorType={x:Type UserControl}}, Path=DataContext.InstructorsObservableCollection}"/>
                    </Style>
                </DataGridComboBoxColumn.EditingElementStyle>
                <DataGridComboBoxColumn.ElementStyle>
                    <Style TargetType="ComboBox">
                        <Setter Property="ItemsSource" Value="{Binding RelativeSource={RelativeSource AncestorType={x:Type UserControl}}, Path=DataContext.InstructorsObservableCollection}"/>
                    </Style>
                </DataGridComboBoxColumn.ElementStyle>
            </DataGridComboBoxColumn>

That all was painful to get through, and thankfully it works quite nicely. Updates work using the DataGridComboBoxColumn selection, but NEW ROWS DO NOT SAVE WITH .SaveChanges()!

Please help me find what I am missing.

Could it perhaps be in this mysterious SelectedItemBinding Property, which I could never get to send change notifications using the backing property on my viewmodel?

Do I need to explicitly tell my bound Classes entity how to save each property? (This doesn't seem reasonable...it should be taken care of with xaml binding it seems.)

Thank you for reading!

Foi útil?

Solução 2

Well, not shockingly, I needed to manually add my entity to the entity set "Classes1" in my ObjectContext. Shame on me for not trying this first, but I assumed that all that detailed DataGrid binding would have provided the capability. My new rule of thumb: If there's any sort of property information being included in an entity by navigation property, I'm going to have to manually make sure those entities are added to the proper Entity Set.

Sigh I'm dumb for not having learned my lesson before...embarrassing...my updated code is below:

 public void SaveClassesWithInstructors()
    {

        foreach(Class1 cl in this._classesCollection.Where(x => x.ClassID == 0))
        {
            var classToCreate = this._context.Classes1.Create();
            classToCreate.InstructorID = ca.InstructorID;
            classToCreate.ClassName = ca.ClassName;
            this._context.Classes1.Add(classToCreate);
            this._context.SaveChanges();
        }
        RaisePropertyChanged("ClassesCollection");
    }

So I guess, at least for anyone who needs a complete how-to on how to do navigational binding and SaveChanges() from DataGrid columns using Entity Framework...here 'tis.

Outras dicas

I think you need use dynamic binging in cs file.

1)If you create binding in cs file, you can pass oject to binding's converterParameter. You can pass ComboxColumn to converter.

2)Use column element to get current selected series id & DataContext(ContactArrangement), and get parent control's DataContext(PNSery collection).

3)use foreach to enum currect PNSery object and assign to ContactArrangment oject.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top