質問

I have entity , build by default from the database table which references to itself

I'm new here and low reputation not alows me to add images, so I'll write all I can.

Area : EntityObject

Properties:

ID

ParentArea_ID

Navigation Properties:

Areas - to collection of Area

ParentArea - to instanse of Area

I want to populate a treeview with data from the database table. I found 2 options: use wrapers or Binding Converter from here. I'm beginner and may be wrong but it seams to me using Binding Converter is better way of doing this.

I found I have to do a new class with Areas collection to make easyer implementation of navigation, adding, deleting

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

And I found I have to do a new Converter class

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();
    }
}

My 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>

And 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;

    }
}

As it is I have an Error The resource "ItemTemplate" could not be resolved.

If I remove ItemTemplate="{StaticResource ItemTemplate}"

from

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

Error is gone but when I try to run the application exeption occurs

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: 

This is my first wpf application and first database design (16 tables). I've spend yet 6 weaks for this project starting from getting started sql server and wpf videos, googling hours and days. My brain starting to boil. I apriciate if answer whould be as detailed as possible.

Thanks for any help!

役に立ちましたか?

解決

The working solution below

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);
    }
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top