Wpf EF6 DatabaseContext和DataTemplate存在问题
!!帖子已经更新,请看下面 所以,6小时后我又需要帮助了 情况: -给定的EF6数据库上下文。(工作) -使用MVVM方法 重要部分构成数据库上下文:Wpf EF6 DatabaseContext和DataTemplate存在问题,wpf,data-binding,mvvm,datatemplate,dbcontext,Wpf,Data Binding,Mvvm,Datatemplate,Dbcontext,!!帖子已经更新,请看下面 所以,6小时后我又需要帮助了 情况: -给定的EF6数据库上下文。(工作) -使用MVVM方法 重要部分构成数据库上下文: public class DatabaseContext : DbContext { public DbSet<Operation> Operations { get; set; } public DbSet<Serie> Series { get; set; } } Operat
public class DatabaseContext : DbContext
{
public DbSet<Operation> Operations { get; set; }
public DbSet<Serie> Series { get; set; }
}
Operation.cs的重要部分
public class Operation
{
public Operation()
{
this.Series = new ObservableCollection<Serie>();
}
public int OperationId { get; set; }
public String Name { get; set; }
public virtual ObservableCollection<Serie> Series { get; set; }
}
公共类操作
{
公共业务()
{
this.Series=新的ObservableCollection();
}
公共int操作ID{get;set;}
公共字符串名称{get;set;}
公共虚拟可观测集合系列{get;set;}
}
现在有一个WPF页面“BestandPage.xaml”:
其视图模型:
public ObservableCollection<Operation> Operations { get { return this._Operations; } set { } }
private ObservableCollection<Operation> _Operations = new ObservableCollection<Operation>();
public BestandPageViewModel()
{
using (var _context = new ProdPlanNET.Models.DatabaseContext())
{
_context.Operations.Load();
var a = _context.Operations.Local;
this.RaisePropertyChanged("Operations");
}
}
public observetecollection操作{get{返回这个。_Operations;}set{}
私有ObservableCollection_Operations=新ObservableCollection();
public BestandPageViewModel()
{
使用(var _context=new ProdPlanNET.Models.DatabaseContext())
{
_context.Operations.Load();
var a=_context.Operations.Local;
本.RaiseProperty变更(“运营”);
}
}
我想要一个像这样的风景
Operation1 Operation2 Operation3 (Label)
------------------------------------
Serie1.1 Serie2.1 Serie3.1 (ListView, containing the series of each operation)
Serie1.2 Serie3.1 Serie3.2
Operation1 Operation2 Operation3 (Label)
------------------------------------
Serie1.1 Serie2.1 Serie3.1 (ListView, containing the series of each operation)
Serie1.2 Serie3.1 Serie3.2
操作1操作2操作3(标签)
------------------------------------
Serie1.1 Serie2.1 Serie3.1(列表视图,包含每个操作的系列)
系列1.2系列3.1系列3.2
它不起作用。
我看到了操作名称,但不知道如何显示相应的序列。
我不确定问题是绑定还是ViewModel
!!!!!更新
我更改了我的ViewModel,如下所示:
class BestandPageViewModel : BaseViewModel
{
public ObservableCollection<ViewObject> ViewObjects { get { return this._ViewObjects; } set { } }
private ObservableCollection<ViewObject> _ViewObjects = new ObservableCollection<ViewObject>();
public BestandPageViewModel()
{
using (var _context = new ProdPlanNET.Models.DatabaseContext())
{
foreach ( Operation ops in _context.Operations ){
_ViewObjects.Add(new ViewObject() { Name = ops.Name, Amount=ops.Series.Count.ToString(), Series = ops.Series.Select(s=>s.SerieNr) });
}
MessageBox.Show(this._ViewObjects.Count.ToString());
this.RaisePropertyChanged("Operations");
}
}
public class ViewObject
{
public String Name { get; set; }
public String Amount { get; set; }
public IEnumerable<String> Series { get; set; }
public ViewObject()
{
this.Series = new ObservableCollection<String>();
}
}
}
class BestandPageViewModel:BaseViewModel
{
public observeCollection ViewObjects{get{返回此值。_ViewObjects;}set{}
私有ObservableCollection_ViewObjects=新ObservableCollection();
public BestandPageViewModel()
{
使用(var _context=new ProdPlanNET.Models.DatabaseContext())
{
foreach(操作操作在_context.Operations中){
_添加(新的ViewObject(){Name=ops.Name,Amount=ops.Series.Count.ToString(),Series=ops.Series.Select(s=>s.SerieNr)});
}
Show(this._ViewObjects.Count.ToString());
本.RaiseProperty变更(“运营”);
}
}
公共类ViewObject
{
公共字符串名称{get;set;}
公共字符串金额{get;set;}
公共IEnumerable系列{get;set;}
公共视图对象()
{
this.Series=新的ObservableCollection();
}
}
}
我创建“视图对象”。这是一个“好”的解决方案吗
现在的问题只是DataTemplate,我想像前面提到的那样
Operation1 Operation2 Operation3 (Label)
------------------------------------
Serie1.1 Serie2.1 Serie3.1 (ListView, containing the series of each operation)
Serie1.2 Serie3.1 Serie3.2
Operation1 Operation2 Operation3 (Label)
------------------------------------
Serie1.1 Serie2.1 Serie3.1 (ListView, containing the series of each operation)
Serie1.2 Serie3.1 Serie3.2
操作1操作2操作3(标签)
------------------------------------
Serie1.1 Serie2.1 Serie3.1(列表视图,包含每个操作的系列)
系列1.2系列3.1系列3.2
当时看起来像:
Operation1
---------
Serie1.1
Serie1.2
Serie1.3
Operation2
---------
Serie2.1
Serie2.2
Serie2.1
操作1
---------
系列1.1
系列1.2
系列1.3
操作2
---------
系列2.1
系列2.2
系列2.1
XAML代码:
<Grid>
<ItemsControl ItemsSource="{Binding ViewObjects}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<StackPanel Orientation="Vertical">
<Label Content="{Binding Name }" />
<Label Content="{Binding Amount }" />
<ListView ItemsSource="{Binding Series}"></ListView>
</StackPanel>
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Grid>
找到了解决方案:
<Grid>
<ItemsControl ItemsSource="{Binding ViewObjects}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical">
<Label Content="{Binding Name }" />
<Label Content="{Binding Amount }" />
<ListView ItemsSource="{Binding Series}"></ListView>
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Grid>
我想我不是WPF的朋友。晚安