【问题标题】:Problems with EF6-DatabaseContext and DataTemplateEF6-DatabaseContext 和 DataTemplate 的问题
【发布时间】:2014-04-03 21:27:12
【问题描述】:

!!帖子更新了,请看下文!!

所以,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 页面“BestandPage.xaml”:

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

它的视图模型:

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

        }

    }

我想要一个类似的视图

操作1 操作2 操作3(标签) ---------------------------------- Serie1.1 Serie2.1 Serie3.1(ListView,包含各个操作的系列) Serie1.2 Serie3.1 Serie3.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>();
        }

    }

}

我创建“ViewObjects”。这是一个“不错”的解决方案吗?

现在问题只是 DataTemplate,我想要一个已经提到的视图

操作1 操作2 操作3(标签) ---------------------------------- Serie1.1 Serie2.1 Serie3.1(ListView,包含各个操作的系列) Serie1.2 Serie3.1 Serie3.2

当时的样子:

操作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>

【问题讨论】:

    标签: wpf data-binding mvvm datatemplate dbcontext


    【解决方案1】:

    找到解决方案:

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

    【讨论】:

      猜你喜欢
      • 2010-12-19
      • 1970-01-01
      • 1970-01-01
      • 2016-02-10
      • 1970-01-01
      • 2011-11-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多