使用Binding Converter和ObservableCollection自我引用实体到wpf treeview

时间:2013-05-15 22:10:16

标签: wpf binding treeview entity self-reference

我有实体,默认情况下从引用自身的数据库表构建

我是新来的,低声望不会让我添加图片,所以我会尽我所能。

Area:EntityObject

属性:

ID

ParentArea_ID

导航属性:

区域 - 收集区域

ParentArea - 到区域

的实例

我想用数据库表中的数据填充树视图。我找到了两个选项:使用来自here的wrapers或Binding Converter。我是初学者,可能是错的,但它使用Binding Converter连接我是更好的方法。

我发现我必须使用Areas集合创建一个新类,以便轻松实现导航,添加,删除

class AreasCollection:ObservableCollection<Area>
{
    private DBEntities _context;

    public DBEntities Context
    {
        get { return _context; }
    }

    public AreasCollection(IEnumerable<Area> Areas, DBEntities context)
        : base(Areas)
    {
        _context = context;
        foreach (Area a in Areas)
        {
            a.Areas.AssociationChanged +=new CollectionChangeEventHandler(Areas_AssociationChanged);
        }
    }

    void Areas_AssociationChanged(object sender, CollectionChangeEventArgs e)
    {
        if (e.Action == CollectionChangeAction.Remove)
        {
            this.Context.DeleteObject((Area)e.Element);
            this.Context.DeleteObject((SpareDevice)e.Element);
            this.Context.DeleteObject((AreaDevice)e.Element);
            this.Context.DeleteObject((Executor)e.Element);
        }
    }

    protected override void InsertItem(int index, Area item)
    {
        item.Areas.AssociationChanged += new CollectionChangeEventHandler(Areas_AssociationChanged);
        this.Context.AddToAreas(item);
        base.InsertItem(index, item);
    }

    protected override void RemoveItem(int index)
    {
        Area a = this[index];
        a.Areas.AssociationChanged -= Areas_AssociationChanged;
        for (int i = a.Areas.Count - 1; i >= 0; i--)
        {
            this.Context.DeleteObject(a.Areas.ElementAt(i));
        } 
        for (int i = a.SpareDevices.Count - 1; i >= 0; i--)
        {
            this.Context.DeleteObject(a.SpareDevices.ElementAt(i));
        }
        for (int i = a.AreaDevices.Count - 1; i >= 0; i--)
        {
            this.Context.DeleteObject(a.AreaDevices.ElementAt(i));
        }
        for (int i = a.Executors.Count - 1; i >= 0; i--)
        {
            this.Context.DeleteObject(a.Executors.ElementAt(i));
        }
        this.Context.DeleteObject(this[index]);
        base.RemoveItem(index);
    }
}

我发现我必须做一个新的转换器类

public class AreasConverter : IValueConverter
{
    public object Convert(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        var item = value as Area;
        return item.Areas.Where(a => a.ParentArea_ID == item.ID);
    }

    public object ConvertBack(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

我的XAML:

    <TreeView Name="AreasTreeView" Height="350" ItemsSource="{Binding Converter={StaticResource AreasConverter}}"  ItemTemplate="{StaticResource ItemTemplate}">
    <TreeView.Resources>
        <local:AreasConverter x:Key="AreasConverter"/>
        <HierarchicalDataTemplate x:Key="ItemTemplate" ItemsSource="{Binding Converter={StaticResource AreasConverter}}">
            <TextBlock Text="{Binding Path=ShortName}" />
        </HierarchicalDataTemplate>
    </TreeView.Resources>
    </TreeView>

和XAML.cs

public partial class MainWindow : Window
{
    private DBEntities context = new DBEntities();
    private CollectionViewSource AreasConverterViewSourse;
    private ListCollectionView AreasConverter;


    public MainWindow()
    {
        InitializeComponent();
        AreasTreeView.ItemsSource = context.Areas.Where(a => a.ParentArea_ID == null);
        AreasConverterViewSourse = (CollectionViewSource)FindResource("AreasConverter");
        this.AreasConverter = (ListCollectionView)this.AreasConverterViewSourse.View;

    }
}

因为它有错误资源“ItemTemplate”无法解析。

如果我删除ItemTemplate =“{StaticResource ItemTemplate}”

来自

    <TreeView Name="AreasTreeView" Height="350" ItemsSource="{Binding Converter={StaticResource AreasConverter}}"  ItemTemplate="{StaticResource ItemTemplate}">

错误消失但当我尝试运行应用程序时出现

System.Windows.Markup.XamlParseException occurred
  HResult=-2146233087
  Message='Provide value on 'System.Windows.StaticResourceExtension' threw an exception.' Line number '30' and line position '61'.
  Source=PresentationFramework
  LineNumber=30
  LinePosition=61
  StackTrace:
      at System.Windows.Markup.WpfXamlLoader.Load(XamlReader xamlReader, IXamlObjectWriterFactory writerFactory, Boolean skipJournaledProperties, Object rootObject, XamlObjectWriterSettings settings, Uri baseUri) 
      at System.Windows.Markup.WpfXamlLoader.LoadBaml(XamlReader xamlReader, Boolean skipJournaledProperties, Object rootObject, XamlAccessLevel accessLevel, Uri baseUri)
      at System.Windows.Markup.XamlReader.LoadBaml(Stream stream, ParserContext parserContext, Object parent, Boolean closeStream)
      at System.Windows.Application.LoadComponent(Object component, Uri resourceLocator)
      at Maintenance.MainWindow.InitializeComponent() in  c:\DATA\Maintenance\Maintenance\MainWindow.xaml:line 1
      at Maintenance.MainWindow..ctor() in c:\DATA\Maintenance\Maintenance\MainWindow.xaml.cs:line 44
  InnerException: 
       HResult=-2146233088
       Message=Cannot find resource named 'AreasConverter'. Resource names are case sensitive.
       Source=PresentationFramework
       StackTrace:
            at System.Windows.StaticResourceExtension.ProvideValue(IServiceProvider serviceProvider)
            at MS.Internal.Xaml.Runtime.ClrObjectRuntime.CallProvideValue(MarkupExtension me, IServiceProvider serviceProvider)
       InnerException: 

这是我的第一个wpf应用程序和第一个数据库设计(16个表)。从开始使用sql server和wpf视频,谷歌搜索时间和日期开始,我已经为这个项目花费了6个弱点。我的大脑开始沸腾。如果答案尽可能详细,我会很乐意。

感谢您的帮助!

1 个答案:

答案 0 :(得分:2)

下面的工作解决方案

XAML:

    <Window.Resources>
        <local:AreasConverter x:Key="AreasConverter"/>
    </Window.Resources>


            <TreeView Name="AreasTreeView" Height="350">
                <TreeView.ItemTemplate>
                    <HierarchicalDataTemplate ItemsSource="{Binding Converter={StaticResource AreasConverter}}">
                        <TextBlock Text="{Binding Path=ShortName}" />
                    </HierarchicalDataTemplate>
                </TreeView.ItemTemplate>
            </TreeView>

ObservableCollection cs:

class AreasCollection:ObservableCollection<Area>
{
    private DBEntities _context;

    public DBEntities Context
    {
        get { return _context; }
    }

    public AreasCollection(IEnumerable<Area> Areas, DBEntities context)
        : base(Areas)
    {
        _context = context;
        foreach (Area a in Areas)
        {
            a.Areas.AssociationChanged +=new CollectionChangeEventHandler(Areas_AssociationChanged);
        }
    }

    void Areas_AssociationChanged(object sender, CollectionChangeEventArgs e)
    {
        if (e.Action == CollectionChangeAction.Remove)
        {
            this.Context.DeleteObject((Area)e.Element);
            this.Context.DeleteObject((SpareDevice)e.Element);
            this.Context.DeleteObject((AreaDevice)e.Element);
            this.Context.DeleteObject((Executor)e.Element);
        }
    }

    protected override void InsertItem(int index, Area item)
    {
        item.Areas.AssociationChanged += new CollectionChangeEventHandler(Areas_AssociationChanged);
        this.Context.AddToAreas(item);
        base.InsertItem(index, item);
    }

    protected override void RemoveItem(int index)
    {
        Area a = this[index];
        a.Areas.AssociationChanged -= Areas_AssociationChanged;
        for (int i = a.Areas.Count - 1; i >= 0; i--)
        {
            this.Context.DeleteObject(a.Areas.ElementAt(i));
        } 
        for (int i = a.SpareDevices.Count - 1; i >= 0; i--)
        {
            this.Context.DeleteObject(a.SpareDevices.ElementAt(i));
        }
        for (int i = a.AreaDevices.Count - 1; i >= 0; i--)
        {
            this.Context.DeleteObject(a.AreaDevices.ElementAt(i));
        }
        for (int i = a.Executors.Count - 1; i >= 0; i--)
        {
            this.Context.DeleteObject(a.Executors.ElementAt(i));
        }
        this.Context.DeleteObject(this[index]);
        base.RemoveItem(index);
    }
}

Converter Class cs:

public class AreasConverter : IValueConverter
{
    public object Convert(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        var item = value as Area;
        return item.Areas.Where(a => a.ParentArea_ID == item.ID);
    }

    public object ConvertBack(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

XAML.cs:

    public MainWindow()
    {
        InitializeComponent();
        AreasTreeView.ItemsSource = context.Areas.Where(a => a.ParentArea_ID == null);
    }