0

::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!

4

2 回答 2

0

好吧,并不令人震惊,我需要手动将我的实体添加到我的 ObjectContext 中的实体集“Classes1”中。我为没有先尝试这个而感到羞耻,但我认为所有详细的 DataGrid 绑定都会提供该功能。我的新经验法则:如果通过导航属性将任何类型的属性信息包含在实体中,我将不得不手动确保将这些实体添加到正确的实体集中。

叹息我之前没有吸取教训而感到愚蠢......尴尬......我更新的代码如下:

 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");
    }

所以我想,至少对于任何需要完整指南的人来说,如何使用实体框架从 DataGrid 列进行导航绑定和 SaveChanges() ......这里是。

于 2013-06-13T22:58:18.360 回答
0

我认为您需要在 cs 文件中使用动态绑定。

1)如果你在cs文件中创建绑定,你可以将oject传递给绑定的converterParameter。您可以将 ComboxColumn 传递给转换器。

2)使用column元素获取当前选中的系列id & DataContext(ContactArrangement),并获取父控件的DataContext(PNSery集合)。

3)使用 foreach 枚举当前 PNSery 对象并分配给 ContactArrangment 对象。

于 2013-06-13T07:28:28.317 回答