EF6-DatabaseContext和DataTemplate的问题

时间:2014-04-03 21:27:12

标签: 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; }
    }

Serie.cs的重要部分

public class Serie
{
    public Int64 SerieId { get; set; }
    [StringLength(15)]
    public String SerieNr { get; set; }
    public virtual Operation CurrentOperation { 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; }

}

现在有一个WPF-Page&#34; BestandPage.xaml&#34;:

<Grid>
    <ItemsControl ItemsSource="{Binding Operations}">
        <ItemsControl.ItemTemplate>
        <DataTemplate>
                <StackPanel>
                    <Label Content="{Binding Name }" />
                    <ListView ItemsSource="{Binding Series}"></ListView>
                 </StackPanel>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
    </ItemsControl>
</Grid>

其ViewModel:

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

        }

    }

我想要一个像

这样的视图
Operation1   Operation2   Operation3  (Label)
------------------------------------
Serie1.1     Serie2.1     Serie3.1    (ListView, containing the series of each operation)
Serie1.2     Serie3.1     Serie3.2

它不起作用。 我看到了操作名称,但我不知道如何显示相应的系列。 我不确定绑定或ViewModel的问题。

!!!!! UPDATE !!!!!

我改变了我的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>();
        }

    }

}

我创建&#34; ViewObjects&#34;。这是一个很好的&#34;溶液

现在问题只是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
---------
Serie1.1
Serie1.2
Serie1.3

Operation2
---------
Serie2.1
Serie2.2
Serie2.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>

1 个答案:

答案 0 :(得分:0)

找到解决方案:

<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的朋友。晚安。