C# 排序第二级树状视图并查看更改。。。可能的

C# 排序第二级树状视图并查看更改。。。可能的,c#,wpf,entity-framework,sorting,refresh,C#,Wpf,Entity Framework,Sorting,Refresh,这是我在这里的第一篇文章,我一直在尝试其他问题中发现的一些技巧,但似乎无法让它像我想要的那样工作。。。 我正在更改现有的应用程序(.net 3.5与WPF和C#,实体框架与sqlserver2008)。我对EFDM和WPF都是新手。 新版本需要与以前版本中的现有数据库完全兼容,而不对现有数据库进行任何修改,因此我不太愿意更改数据模型及其生成的任何对象 无论如何,我的问题是: 我有来自edm的对象“Stafficentive”和“Stafficentiveline”,每个Stafficentive

这是我在这里的第一篇文章,我一直在尝试其他问题中发现的一些技巧,但似乎无法让它像我想要的那样工作。。。 我正在更改现有的应用程序(.net 3.5与WPF和C#,实体框架与sqlserver2008)。我对EFDM和WPF都是新手。 新版本需要与以前版本中的现有数据库完全兼容,而不对现有数据库进行任何修改,因此我不太愿意更改数据模型及其生成的任何对象

无论如何,我的问题是: 我有来自edm的对象“Stafficentive”和“Stafficentiveline”,每个Stafficentive都连接了0到多个Stafficentiveline。 我将它们显示为树状视图,并且需要能够动态添加或删除StafficenTiveline


这很好,但是StatfficenTiveline没有排序,但是它们确实需要按顺序显示(升序lbound)。 所以我寻找了一个解决方案,找到了一个转换器

public object Convert(object value, Type targetType, object parameter,
System.Globalization.CultureInfo culture)
        {return
((EntityCollection<staffincentiveline>)value).OrderBy(o => o.lbound);}
公共对象转换(对象值、类型targetType、对象参数、, System.Globalization.culture(信息文化) {返回 ((EntityCollection)值).OrderBy(o=>o.lbound);} 然后我意识到,现在当我使用屏幕上的按钮添加或删除StafficenTiveline时,更改不会显示出来。如果我理解正确的话,那是因为incentiveline是通过排序转换器提供的视图显示的,当对集合进行更改时,视图不会刷新。如果我再次理解正确,这是因为EntityCollection没有实现INotifyPropertyChanged。 如果我使用
CollectionViewSource.GetDefaultView(_).Refresh();然后所有的树状视图都被刷新,所有的项目都被折叠起来,这对用户来说是相当混乱的

那么,在不更改对象类Stafficentive和Stafficentiveline的情况下,如何使排序和刷新工作正常呢?我应该创建另一个包含它们的类并实现INotifyPropertyChanged吗

谢谢

我设法做到了:)

我创建了两个类StatfficeEntiveHelperObject和StatfficeEntiveLineHelperObject,它们实现了INotifyPropertyChanged。当通过访问器进行更改时,它们与现有类和激发事件重叠。 正如您可以看到的那样,有一些方法可以在StafficenTiveHelperObject中从ObservableCollection添加/删除StafficenTivelineHelperObject,这些方法还可以从EDM中添加/删除原始对象。。。这是有效地将对视图所做的更改链接到数据库的位

public class StaffIncentiveHelperObject : INotifyPropertyChanged//
    {
        public delegate void CollectionChangedDelegate();
        public static CollectionChangedDelegate CollectionChanged;

        public StaffIncentiveHelperObject(staffincentive input)
        {
            this._StaffIncentive = input;
        }


        private staffincentive _StaffIncentive;
        public staffincentive StaffIncentive
        {
            get { return _StaffIncentive; }
            set
            {
                if (_StaffIncentive == value) return;
                _StaffIncentive = value;
                OnPropertyChanged("StaffIncentive");
            }
        }

        public decimal? StaffIncentiveIncrement
        {
            get 
            { 
                return _StaffIncentive.increment; 
            }
            set
            {
                if (_StaffIncentive.increment != value)
                {
                    _StaffIncentive.increment = value;
                    OnPropertyChanged("StaffIncentive");
                }
            }
        }

        public string StaffIncentiveName
        {
            get
            {
                return _StaffIncentive.name;
            }
            set
            {
                if (_StaffIncentive.name != value)
                {
                    _StaffIncentive.name = value;
                    OnPropertyChanged("StaffIncentive");
                }
            }
        }

        public byte? StaffIncentiveType
        {
            get
            {
                return _StaffIncentive.type;
            }
            set
            {
                if(_StaffIncentive.type != value)
                {
                    _StaffIncentive.type = value;
                    OnPropertyChanged("StaffIncentive");
                }
            }
        }

        private ObservableCollection<StaffIncentiveLineHelperObject> _StaffIncentiveLines = new ObservableCollection<StaffIncentiveLineHelperObject>();
        public ObservableCollection<StaffIncentiveLineHelperObject> StaffIncentiveLines
        {
            get { return _StaffIncentiveLines; }
            set { _StaffIncentiveLines = value; OnPropertyChanged("StaffIncentiveLines"); }
        }


        public void AddStaffIncentiveLine( staffincentiveline SIL)
        {
            SIL.staffincentive = this._StaffIncentive;
            _StaffIncentive.staffincentiveline.Add(SIL);
            StaffIncentiveLines.Add(new StaffIncentiveLineHelperObject(SIL, this));
            //OnPropertyChanged("StaffIncentiveLine");
            if (CollectionChanged != null)
            {
                CollectionChanged.Invoke();
            }

        }

        public void RemoveStaffIncentiveLine(StaffIncentiveLineHelperObject SILHO)
        {
            _StaffIncentive.staffincentiveline.Remove(SILHO.StaffIncentiveLine);
            _StaffIncentiveLines.Remove(SILHO);
            //OnPropertyChanged("StaffIncentiveLine");
            if (CollectionChanged != null)
            {
                CollectionChanged.Invoke();
            }

        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        private void OnPropertyChanged(string name)
        {
            var x = PropertyChanged;
            if (x != null)
                x(this, new PropertyChangedEventArgs(name));
        }

        #endregion
    }

    public class StaffIncentiveLineHelperObject : INotifyPropertyChanged//, INotifyCo
    {
        public StaffIncentiveLineHelperObject(staffincentiveline input, StaffIncentiveHelperObject parent)
        {
            this._StaffIncentiveLine = input;
            this.Parent = parent;
        }


        public StaffIncentiveHelperObject Parent { get; set;}

        private staffincentiveline _StaffIncentiveLine;
        public staffincentiveline StaffIncentiveLine
        {
            get { return _StaffIncentiveLine; }
            set
            {
                if (_StaffIncentiveLine == value) return;
                _StaffIncentiveLine = value;
                OnPropertyChanged("StaffIncentiveLine");
            }
        }

        public decimal? StaffIncentiveLineLbound
        {
            get
            {
                return _StaffIncentiveLine.lbound;
            }
            set
            {
                if (_StaffIncentiveLine.lbound != value)
                {
                    _StaffIncentiveLine.lbound = value;
                    OnPropertyChanged("StaffIncentiveLine");
                }
            }
        }

        public double? StaffIncentiveLinePercentage
        {
            get
            {
                return _StaffIncentiveLine.percentage;
            }
            set
            {
                if (_StaffIncentiveLine.percentage != value)
                {
                    _StaffIncentiveLine.percentage = value;
                    OnPropertyChanged("StaffIncentiveLine");
                }
            }
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        private void OnPropertyChanged(string name)
        {
            var x = PropertyChanged;
            if (x != null)
                x(this, new PropertyChangedEventArgs(name));

        }

        #endregion
    }
如果其他人有更好的方法或手段来改进这一点,我很乐意听到:)

ObservableCollection<SupportingObjects.StaffIncentiveHelperObject> myIcentives = new ObservableCollection<SupportingObjects.StaffIncentiveHelperObject>();
foreach (staffincentive SI in db_incentives)
{
    StaffIncentiveHelperObject SIHO = new StaffIncentiveHelperObject(SI);
    myIcentives.Add(SIHO);
    foreach (staffincentiveline SIL in SI.staffincentiveline)
    {
        SIHO.AddStaffIncentiveLine(SIL);
    }
}
<TreeView ItemsSource="{Binding}" Name="MainTree">
<TreeView.ItemTemplate>
    <HierarchicalDataTemplate ItemsSource="{Binding StaffIncentiveLines, Converter={StaticResource IncentiveLineHelperObjectSort}, ConverterParameter=StaffIncentiveLineLbound}">
        <StackPanel Orientation="Horizontal" >
            <TextBox Text="{Binding StaffIncentiveName}" Width="120" />
            <Button Tag="{Binding}" Click="addline" Content="Add Line" />
        </StackPanel>
        <HierarchicalDataTemplate.ItemTemplate>
            <DataTemplate >
                <StackPanel Orientation="Horizontal"  Margin="150,0,0,0">
                    <TextBox Text="{Binding StaffIncentiveLineLbound, StringFormat='{}{0:c}'}" Width="120" />
                    <TextBox Text="{Binding StaffIncentiveLinePercentage}" Width="120" />
                <Button Tag="{Binding}" Click="delLine" Content="Remove"/>
                </StackPanel>
            </DataTemplate>
        </HierarchicalDataTemplate.ItemTemplate>
    </HierarchicalDataTemplate>
</TreeView.ItemTemplate>
public class IncentiveLineHelperObjectSort : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            System.Collections.ObjectModel.ObservableCollection<StaffIncentiveLineHelperObject> collection = value as System.Collections.ObjectModel.ObservableCollection<StaffIncentiveLineHelperObject>;
            ListCollectionView view = new ListCollectionView(collection);
            System.ComponentModel.SortDescription sort = new System.ComponentModel.SortDescription(parameter.ToString(), System.ComponentModel.ListSortDirection.Ascending);
            view.SortDescriptions.Add(sort);

            return view;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return null;
        }
    }
private void addline(object sender, RoutedEventArgs e)
        {
            var incentive = ((sender as Button).Tag as StaffIncentiveHelperObject);
            incentive.AddStaffIncentiveLine(new staffincentiveline());
            TreeViewItem thisTreeViewItem = MainTree.ItemContainerGenerator.ContainerFromItem(incentive) as TreeViewItem;
            thisTreeViewItem.IsExpanded = true;
        }

        private void delLine(object sender, RoutedEventArgs e)
        {
            var line = ((sender as Button).Tag as StaffIncentiveLineHelperObject);
            StaffIncentiveHelperObject thisIncentive = line.Parent;

            thisIncentive.RemoveStaffIncentiveLine(line);
            TreeViewItem thisTreeViewItem = MainTree.ItemContainerGenerator.ContainerFromItem(thisIncentive) as TreeViewItem;
            if (thisIncentive.StaffIncentiveLines.Count == 0)
            {

                thisTreeViewItem.IsExpanded = false;
            }
        }