Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wpf 如何使用MVVM将DataGridComboxColumn绑定到EntityFramework?_Wpf_Vb.net_Entity Framework_Mvvm - Fatal编程技术网

Wpf 如何使用MVVM将DataGridComboxColumn绑定到EntityFramework?

Wpf 如何使用MVVM将DataGridComboxColumn绑定到EntityFramework?,wpf,vb.net,entity-framework,mvvm,Wpf,Vb.net,Entity Framework,Mvvm,我正在尝试让DataGridComboxColumn与我的ViewModel一起工作。看起来一切正常,但当我更改组合框的值时,实体没有更改 窗口的datacontext具有以下属性: 项目资源 Public Property AllEnergySources() As ObservableCollection(Of EnergySourceViewModel) 选择编辑绑定 Private _CurrentEnergySource As EnergySourceViewModel

我正在尝试让DataGridComboxColumn与我的ViewModel一起工作。看起来一切正常,但当我更改组合框的值时,实体没有更改

窗口的datacontext具有以下属性:

项目资源

Public Property AllEnergySources() As ObservableCollection(Of EnergySourceViewModel)
选择编辑绑定

  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
我觉得问题在于如何在作为DataContext的ViewModel中填充CurrentEnergySource:

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
当我为组合框创建备份集合时,如果模型是选定的,我将该viewmodel设置为CurrentEnergySource,但在该点之后,它被断开(这就是问题所在)


我应该在CurrentEnergySource中引用什么,以便它在组合框更改时更新模型?

当然,问题在于您的绑定,DataGridComboBoxColumn自动从CurrentEnergySource中获取一项,而CurrentEnergySource上没有AllEnergySources,如果您不使用它



有一点似乎是错误的,您可能应该使用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; }
}

公共类数据项:ViewModelBase
{
公共int Id{get;set;}
公共字符串名称{get;set;}
公共字符串说明{get;set;}
私有DetailItem\u DetailItem;
公共详情项目详情项目
{
获取{return\u detailItem;}
设置
{
Debug.WriteLine(值!=null)
?string.Format(“将详细信息项设置为:{0}”,value.Name)
:“将详细信息项设置为空。”);
设置(()=>DetailItem,ref\u DetailItem,value);
}
}
}
公共类详细信息项:ViewModelBase
{
公共int Id{get;set;}
公共字符串名称{get;set;}
}
公共类DataGridComboxViewModel:ViewModelBase
{
公共DataGridComboxViewModel()
{
DetailItems=新列表
{
新的DetailItem{Id=0,Name=“Zero”},
新的DetailItem{Id=1,Name=“One”},
新的DetailItem{Id=2,Name=“Two”},
新的DetailItem{Id=3,Name=“Three”},
};
项目=新列表
{
新数据项{Id=0,Name=“Item 1”,Description=“This is Item 1”},
新数据项{Id=1,Name=“Item 2”,Description=“This is Item 2”},
新数据项{Id=2,Name=“Item 3”,Description=“This is Item 3”},
新数据项{Id=3,Name=“Item 4”,Description=“This is Item 4”},
};
}
公共列表项{get;set;}
公共列表详细信息项{get;private set;}
}

您是否在绑定上尝试了相对资源?通常,当我绑定到模板等中的列时,绑定会查看控件的绑定,而不是视图的datacontext的绑定

尝试以下任一方法:

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


然后将
x:Name=“nameofview”
添加到视图的属性中(在>括号内的xmlns位置下方)

我的答案是您需要手动更改外键(我现在在CurrentEnergySource的setter中更改它,它是SelectedItemBinding绑定属性)


加载时,填充私有备份存储
\u CurrentEnergySource
,而不是属性,以避免所有对象以修改状态开始

是否双向绑定,EnergySourceViewModel是否实现INotifyPropertyChanged?是(&Yes),但问题是我绑定到的属性(CurrentEnergySource)有自己的支持领域。属性应该获取/设置什么而不是备份字段?显示您是否绑定DataGridComboxColumn可能是一个愚蠢的建议,但您是否尝试将
UpdateSourceTrigger=PropertyChanged
放置在绑定中?这并没有回答我的问题,它只是转换为DataGridTemplateColumn而不是DataGridComoBoxColumn。问题是我不知道如何使用SelectedItemFromItemsSourceSorry,我应该提到我使用的是MVVMLight。Set是设置属性和引发属性更改通知的帮助程序。您可以替换为_detailItem=value;RaisePropertyChanged(“详细项目”);(或您的同等产品)。
"{Binding Path=DataContext.CurrentEnergySource, 
          RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}},
          Mode=TwoWay}"
"{Binding ElementName=NameOfTheView, 
          Path=DataContext.CurrentEnergySource, 
          Mode=TwoWay}"
     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