2012-02-16 30 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地方)

相关问题