Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用基础数据中不存在的结构中的项填充WPF TreeView_C#_Wpf_Xaml_Treeview - Fatal编程技术网

C# 使用基础数据中不存在的结构中的项填充WPF TreeView

C# 使用基础数据中不存在的结构中的项填充WPF TreeView,c#,wpf,xaml,treeview,C#,Wpf,Xaml,Treeview,我有一个不同类型的对象列表,我想在WPF树视图中显示这些对象。有些对象应该是其他对象的子对象,但我不希望父对象必须维护自己的子对象列表。我尝试通过实现一个属性来实现这一点,该属性为该组中的子项返回IEnumerator,并将其绑定为ItemsSource,但它似乎不起作用。我创建了以下内容来演示这个问题 政务司司长: XAML: 为了清楚起见,我希望子项显示为具有相同GroupId的ItemGroup的子项。请告诉我最好的方法是什么?您必须对代码进行一些重大更改: 首先,Items.ItemLi

我有一个不同类型的对象列表,我想在WPF树视图中显示这些对象。有些对象应该是其他对象的子对象,但我不希望父对象必须维护自己的子对象列表。我尝试通过实现一个属性来实现这一点,该属性为该组中的子项返回IEnumerator,并将其绑定为ItemsSource,但它似乎不起作用。我创建了以下内容来演示这个问题

政务司司长:

XAML:


为了清楚起见,我希望子项显示为具有相同GroupId的ItemGroup的子项。请告诉我最好的方法是什么?

您必须对代码进行一些重大更改:

首先,Items.ItemList应该只包含ItemGroup类型的元素。现在,它还包含SubItem类型的元素,它们在树视图中显示为父元素。这不是我们想要实现的。 但是,为了跟踪所有项目,我们需要另一个集合items.AllItems。 应修改ItemGroup.Children属性以使用Items.AllItems集合,而不是Items.ItemList。 最后,我们需要在创建新的子项时通知树视图。我们将使用INotifyPropertyChanged来完成此操作。 这是一个工作代码。但是,请注意,它不是生产代码。我只改变了绝对必要的东西。它的目的只是向你展示一个想法

您还应该知道,与父对象有自己的子对象列表时的方法相比,这种解决方案可能会慢一些。它将取决于树状视图中的项目数

public class Items
{
    public List<BaseItem> AllItems { get; private set; }
    public ObservableCollection<BaseItem> ItemList { get; private set; }

    public Items()
    {
        ItemList = new ObservableCollection<BaseItem>();
        AllItems = new List<BaseItem>();
    }

    public void Add(BaseItem item)
    {
        AllItems.Add(item);

        if (item is ItemGroup)
            ItemList.Add(item);
        else
            AllItems.OfType<ItemGroup>().Single(i => i.GroupId == item.GroupId).Notify();
        }
    }

public class ItemGroup : BaseItem, INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    public ItemGroup(Items _List): base(_List) { }

    public IEnumerator Children
    {
        get
        {
            return _List.AllItems
                .OfType<SubItem>()
                .Where(SI => SI.GroupId == this.GroupId)
                .GetEnumerator();
        }
    }

    public void Notify()
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(nameof(Children)));
    }
}

我将接受迈克尔的回答,因为它告诉我我做错了什么,以及如何纠正它。实际上,我找到了一个我更喜欢的不同解决方案,因为它不需要额外的集合:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Data;

namespace WpfApplication3
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private Items MyItems;
        private int NextGroupId = 0;
        public MainWindow()
        {
            InitializeComponent();
            MyItems = new Items();
            tvMain.ItemsSource = MyItems.Groups as IEnumerable;
        }
        private void btnNewGroup_Click(object sender, RoutedEventArgs e)
        {
            MyItems.Add(new ItemGroup(MyItems)
            {
                Name = Guid.NewGuid().ToString(),
                GroupId = NextGroupId
            });
            NextGroupId++;
        }
        private void btnNewSubItem_Click(object sender, RoutedEventArgs e)
        {
            MyItems.Add(new SubItem(MyItems)
            {
                Name = Guid.NewGuid().ToString(),
                GroupId = (tvMain.SelectedItem as ItemGroup).GroupId
            });
        }

        private void tvMain_SelectedItemChanged(object sender,
            RoutedPropertyChangedEventArgs<object> e)
        {
            btnNewSubItem.IsEnabled = tvMain.SelectedItem is ItemGroup;
        }
    }
    public class Items
    {
        public ObservableCollection<BaseItem> ItemList { get; private set; }
        public ICollectionView Groups { get; private set; }
        public Items()
        {
            ItemList = new ObservableCollection<BaseItem>();
            Groups = CollectionViewSource.GetDefaultView(ItemList);
            Groups.Filter = item => item is ItemGroup;
        }
        public void Add(BaseItem item)
        {
            ItemList.Add(item);
            if (item is SubItem)
                ItemList
                    .OfType<ItemGroup>()
                    .Where(g => g.GroupId == item.GroupId)
                    .Single()
                    .NotifyPropertyChanged("Children");
        }
    }
    public class BaseItem : INotifyPropertyChanged
    {
        protected Items _List;
        public BaseItem(Items List)
        {
            _List = List;
        }
        public string Name { get; set; }
        public int GroupId { get; set; }
        public event PropertyChangedEventHandler PropertyChanged;
        public void NotifyPropertyChanged(string propName)
        {
            if (this.PropertyChanged != null)
                this.PropertyChanged(this, new PropertyChangedEventArgs(propName));
        }
    }
    public class ItemGroup : BaseItem
    {
        public ItemGroup(Items _List)
            : base(_List) { }
        public IEnumerator Children
        {
            get
            {
                return _List.ItemList
                    .OfType<SubItem>()
                    .Where(SI => SI.GroupId == this.GroupId)
                    .GetEnumerator();
            }
        }
    }
    public class SubItem : BaseItem
    {
        public SubItem(Items _List)
            : base(_List) { }
    }
}

非常感谢。
public class Items
{
    public List<BaseItem> AllItems { get; private set; }
    public ObservableCollection<BaseItem> ItemList { get; private set; }

    public Items()
    {
        ItemList = new ObservableCollection<BaseItem>();
        AllItems = new List<BaseItem>();
    }

    public void Add(BaseItem item)
    {
        AllItems.Add(item);

        if (item is ItemGroup)
            ItemList.Add(item);
        else
            AllItems.OfType<ItemGroup>().Single(i => i.GroupId == item.GroupId).Notify();
        }
    }

public class ItemGroup : BaseItem, INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    public ItemGroup(Items _List): base(_List) { }

    public IEnumerator Children
    {
        get
        {
            return _List.AllItems
                .OfType<SubItem>()
                .Where(SI => SI.GroupId == this.GroupId)
                .GetEnumerator();
        }
    }

    public void Notify()
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(nameof(Children)));
    }
}
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Data;

namespace WpfApplication3
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private Items MyItems;
        private int NextGroupId = 0;
        public MainWindow()
        {
            InitializeComponent();
            MyItems = new Items();
            tvMain.ItemsSource = MyItems.Groups as IEnumerable;
        }
        private void btnNewGroup_Click(object sender, RoutedEventArgs e)
        {
            MyItems.Add(new ItemGroup(MyItems)
            {
                Name = Guid.NewGuid().ToString(),
                GroupId = NextGroupId
            });
            NextGroupId++;
        }
        private void btnNewSubItem_Click(object sender, RoutedEventArgs e)
        {
            MyItems.Add(new SubItem(MyItems)
            {
                Name = Guid.NewGuid().ToString(),
                GroupId = (tvMain.SelectedItem as ItemGroup).GroupId
            });
        }

        private void tvMain_SelectedItemChanged(object sender,
            RoutedPropertyChangedEventArgs<object> e)
        {
            btnNewSubItem.IsEnabled = tvMain.SelectedItem is ItemGroup;
        }
    }
    public class Items
    {
        public ObservableCollection<BaseItem> ItemList { get; private set; }
        public ICollectionView Groups { get; private set; }
        public Items()
        {
            ItemList = new ObservableCollection<BaseItem>();
            Groups = CollectionViewSource.GetDefaultView(ItemList);
            Groups.Filter = item => item is ItemGroup;
        }
        public void Add(BaseItem item)
        {
            ItemList.Add(item);
            if (item is SubItem)
                ItemList
                    .OfType<ItemGroup>()
                    .Where(g => g.GroupId == item.GroupId)
                    .Single()
                    .NotifyPropertyChanged("Children");
        }
    }
    public class BaseItem : INotifyPropertyChanged
    {
        protected Items _List;
        public BaseItem(Items List)
        {
            _List = List;
        }
        public string Name { get; set; }
        public int GroupId { get; set; }
        public event PropertyChangedEventHandler PropertyChanged;
        public void NotifyPropertyChanged(string propName)
        {
            if (this.PropertyChanged != null)
                this.PropertyChanged(this, new PropertyChangedEventArgs(propName));
        }
    }
    public class ItemGroup : BaseItem
    {
        public ItemGroup(Items _List)
            : base(_List) { }
        public IEnumerator Children
        {
            get
            {
                return _List.ItemList
                    .OfType<SubItem>()
                    .Where(SI => SI.GroupId == this.GroupId)
                    .GetEnumerator();
            }
        }
    }
    public class SubItem : BaseItem
    {
        public SubItem(Items _List)
            : base(_List) { }
    }
}