2012-02-16 56 views
0

我想獲得一個DataGridComboBoxColumn與我的ViewModel一起工作。一切似乎正常工作,但當我更改組合框的值時,該實體不會更改。如何使用MVVM將DataGridComboBoxColumn綁定到EntityFramework?

窗口的DataContext的具有以下屬性:

的ItemsSource

Public Property AllEnergySources() As ObservableCollection(Of EnergySourceViewModel) 

SelectedItemBinding

Private _CurrentEnergySource As EnergySourceViewModel 
    Public Property CurrentEnergySource() As EnergySourceViewModel 
     Get 
      Return _CurrentEnergySource 
     End Get 
     Set(ByVal value As EnergySourceViewModel) 
      _CurrentEnergySource = value 
      OnPropertyChanged("CurrentEnergySource") 
     End Set 
    End Property 

我覺得問題在於我怎麼填充CurrentEnergySource在視圖模型那就是DataContext:

Sub New(SelectedEntity as EquipmentEnergySource) 
    AllEnergySources = New ObservableCollection(Of EnergySourceViewModel) 

    //Select all EnergySources from the EntityFramework 
    Dim EnergyEntities = From esr in db.EnergySources Select esr 

       //Loop through to convert Entity POCO to Collection of ViewModels 
       For Each es In EnergyEntities 
        _AllEnergySources.Add(New EnergySourceViewModel(es)) 

        //Optionally Set the newly created ViewModel to SelectedItemBinding object 
        If es.EnergySourceID = SelectedEntity.EnergySourceID Then 
         _CurrentEnergySource = _AllEnergySources.Last 
        End If 
       Next 
End Sub 

當我創建的後盾收集組合框,如果模型中選擇一個,我設置視圖模型是CurrentEnergySource,但在那之後它斷開(這就是問題所在)

我應該在CurrentEnergySource中引用什麼,以便在組合框更改時更新模型?

+0

你是否雙向綁定和不EnergySourceViewModel執行INotifyPropertyChanged? – Paparazzi 2012-02-16 22:07:18

+0

是的,是的,但問題是我綁定到的屬性(CurrentEnergySource)有其自己的支持領域。而不是支持領域該物業應該得到/設置什麼? – Michael 2012-02-16 22:26:46

+0

顯示你綁定DataGridComboBoxColumn – Paparazzi 2012-02-16 23:21:46

回答

0

我的回答是,你需要手動更改外鍵(我現在在CurrentEnergySource的setter中更改它,這是SelectedItemBinding綁定屬性)

 Private _CurrentEnergySource As EnergySourceViewModel 
    Public Property CurrentEnergySource() As EnergySourceViewModel 
     Get 
      Return _CurrentEnergySource 
     End Get 
     Set(ByVal value As EnergySourceViewModel) 
      _CurrentEnergySource = value 
      Me.Model.EnergySourceID = value.Model.EnergySourceID 
      OnPropertyChanged("CurrentEnergySource") 
     End Set 
    End Property 

上的負載,填充私人後備存儲_CurrentEnergySource,而不是財產,以避免所有的對象開始了與修改後的狀態

0
當然

,問題是在你的綁定,DataGridComboBoxColumn自動從CurrentEnergySource採取一個項目,而CurrentEnergySource沒有關於它AllEnergySources,乳清你不semply使用此,

  <DataGridTemplateColumn> 
       <DataGridTemplateColumn.CellTemplate> 
        <DataTemplate> 
         <TextBlock Text="{Binding SelectedItemFromItemsSource}"/> 
        </DataTemplate> 
       </DataGridTemplateColumn.CellTemplate> 
       <DataGridTemplateColumn.CellEditingTemplate> 
        <DataTemplate> 
         <ComboBox ItemsSource="{Binding ItemsSource}" /> 
        </DataTemplate> 
       </DataGridTemplateColumn.CellEditingTemplate> 
      </DataGridTemplateColumn> 
+0

這不回答我的問題,它只是轉換爲DataGridTemplateColumn而不是DataGridComoBoxColumn。問題是我不知道如何使用SelectedItemFromItemsSource – Michael 2012-02-20 14:45:03

1

有一件事情似乎是錯誤的,你應該使用SelectedValueBinding而不是SelectedItemBinding。

下面是對我工作的很好的例子:

<Page.Resources> 
    <ViewModel:DataGridComboBoxViewModel x:Key="model"/> 
    <Style x:Key="ElementStyle" TargetType="ComboBox"> 
     <Setter 
      Property="ItemsControl.ItemsSource" 
      Value="{Binding Path=DataContext.DetailItems, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}}" 
      /> 
    </Style> 
</Page.Resources> 

<Grid DataContext="{StaticResource model}"> 
    <DataGrid ItemsSource="{Binding Items}" AutoGenerateColumns="False"> 
     <DataGrid.Columns> 
      <DataGridTextColumn Header="Id" Binding="{Binding Id}"/> 
      <DataGridTextColumn Header="Name" Binding="{Binding Name}"/> 
      <DataGridComboBoxColumn Header="Combo" 
            DisplayMemberPath="Name" 
            SelectedValueBinding="{Binding DetailItem}" 
            ElementStyle="{StaticResource ElementStyle}" 
            EditingElementStyle="{StaticResource ElementStyle}" 
            > 
      </DataGridComboBoxColumn> 
     </DataGrid.Columns> 
    </DataGrid> 
</Grid> 

public class DataItem : ViewModelBase 
{ 
    public int Id { get; set; } 
    public string Name { get; set; } 
    public string Description { get; set; } 

    private DetailItem _detailItem; 
    public DetailItem DetailItem 
    { 
     get { return _detailItem; } 
     set 
     { 
      Debug.WriteLine(value != null 
           ? string.Format("Setting detail item to: {0}", value.Name) 
           : "Setting detail item to null."); 

      Set(() => DetailItem, ref _detailItem, value); 
     } 
    } 
} 

public class DetailItem : ViewModelBase 
{ 
    public int Id { get; set; } 
    public string Name { get; set; } 
} 

public class DataGridComboBoxViewModel : ViewModelBase 
{ 
    public DataGridComboBoxViewModel() 
    { 
     DetailItems = new List<DetailItem> 
          { 
           new DetailItem {Id = 0, Name = "Zero"}, 
           new DetailItem {Id = 1, Name = "One"}, 
           new DetailItem {Id = 2, Name = "Two"}, 
           new DetailItem {Id = 3, Name = "Three"}, 
          }; 

     Items = new List<DataItem> 
        { 
         new DataItem {Id = 0, Name = "Item 1", Description = "This is item 1"}, 
         new DataItem {Id = 1, Name = "Item 2", Description = "This is item 2"}, 
         new DataItem {Id = 2, Name = "Item 3", Description = "This is item 3"}, 
         new DataItem {Id = 3, Name = "Item 4", Description = "This is item 4"}, 
        }; 
    } 

    public List<DataItem> Items { get; set; } 
    public List<DetailItem> DetailItems { get; private set; } 
} 
+0

Set()是做什麼的? – Michael 2012-02-22 14:09:41

+0

對不起,應該提到我正在使用MVVMLight。 Set是設置財產和籌集財產變更通知的助手。你可以用_detailItem = value替換; RaisePropertyChanged( 「DetailItem」); (或您的等價物)。 – Phil 2012-02-22 14:17:59

0

您是否嘗試過在綁定的RelativeSource?通常我發現當我綁定到模板中的列時,綁定看起來在控件的綁定內部,而不是視圖的datacontext。

嘗試之一:

"{Binding Path=DataContext.CurrentEnergySource, 
      RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, 
      Mode=TwoWay}" 

"{Binding ElementName=NameOfTheView, 
      Path=DataContext.CurrentEnergySource, 
      Mode=TwoWay}" 

,然後加入x:Name="NameOfTheView"到查看的屬性(以下>支架內的xmlns地方)

相關問題