基于DataGrid的WPF Customcontrol+;自定义事件

基于DataGrid的WPF Customcontrol+;自定义事件,wpf,events,datagrid,custom-controls,Wpf,Events,Datagrid,Custom Controls,我目前正在编写一个基于标准WPF数据网格的Customcontrol。我实现了一个带有排序、分组和筛选等功能的工具栏。使用ICollectionView作为ItemsSource,这些函数相当容易实现 我的问题在于工具栏按钮应触发的事件: 我已经设法通过命令(SortClick、GroupClick、FilterClick)将按钮click事件提升到视图的代码隐藏(MyDataGrid.cs) 请帮忙-也许有一个更简单的解决方案,我现在看不到 哦,我忘了提到我正在MVVM模式下开发我的控件,我

我目前正在编写一个基于标准WPF数据网格的Customcontrol。我实现了一个带有排序、分组和筛选等功能的工具栏。使用ICollectionView作为ItemsSource,这些函数相当容易实现

我的问题在于工具栏按钮应触发的事件: 我已经设法通过命令(SortClick、GroupClick、FilterClick)将按钮click事件提升到视图的代码隐藏(MyDataGrid.cs)

请帮忙-也许有一个更简单的解决方案,我现在看不到


哦,我忘了提到我正在MVVM模式下开发我的控件,我想坚持下去。整个逻辑(过滤器、组、排序)应该在我的viewmodel中

编辑:
哦,我忘了提到我正在MVVM模式下开发我的控件,我想坚持下去。整个逻辑(过滤器、组、排序)应该在我的viewmodel中。

我没有完全理解。是什么阻止您将按钮命令绑定到viewmodel并在viewmodel中发挥神奇作用?(如CollectionView排序等)。通常直接绑定到DataContext和viewmodel。没有代码隐藏。

Hm,我很难解释-我的英语有点生疏-但我会试试。一方面,我有一个CustomDataGrid,其中添加了按钮='Control View'。另一侧是my Main Window=View,它绑定到一个ViewModel。我的理解是,我需要将筛选、排序和分组的所有逻辑放在我的控件视图中。但此“控件视图”无法访问ItemSource,它位于我的主窗口的ViewModel中。我希望我的问题现在更清楚了——如果不是的话,请毫不犹豫地问!如果您现在能在ViewModel中提供帮助,实现SortClick命令,然后实现逻辑,如:YourCollection.Sort();也就是说,假设您的控件是可视化树&main窗口的一部分,那么我就必须在它之外执行属于CustomDataGrid的所有逻辑。每个使用该控件的人不仅需要处理ICollectionView.Sort等,还需要为每个函数编写一个窗口来配置它(例如,Sort config窗口由一个datagrid组成,您可以在其中选择列和排序方向)。因此,不幸的是,将事件抛出到主窗口的ViewModel中对我没有帮助:/将SortClickCommand添加到CustomDataGri中。现在,将新的事件处理程序添加到CustomDataGrid.cs中,例如SortButtonClicked,并在其中执行SortClickCommand.Execute()。这让每个人都可以随心所欲地覆盖SortClickCommand的功能。
                                    <ToolBarTray Orientation="Vertical" Grid.Row="1" Grid.Column="2" IsLocked="True">
                                        <ToolBar Band="1" BandIndex="1" >
                                            <Button Width="24" Height="24" ToolTip="Sort" Command="{x:Static local:PDataGrid.SortClick}">
                                                <Image Source="pack://application:,,,/PControls;component/Resources/sort.png" />
                                            </Button>
                                            <Button Width="24" Height="24" ToolTip="Filter" Command="{x:Static local:PDataGrid.GroupClick}">
                                                <Image Source="pack://application:,,,/PControls;component/Resources/filter.png" />
                                            </Button>
                                            <Button Width="24" Height="24" ToolTip="Group" Command="{x:Static local:PDataGrid.FilterClick}">
                                                <Image Source="pack://application:,,,/PControls;component/Resources/group.png" />
                                            </Button>
                                        </ToolBar>
                                    </ToolBarTray>
    public static readonly RoutedEvent SortClickEvent = EventManager.RegisterRoutedEvent("SClick", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(PDataGrid));

    public event RoutedEventHandler SClick
    {
        add { AddHandler(SortClickEvent, value); }
        remove { RemoveHandler(SortClickEvent, value); }
    }

    private void raiseSortClickEvent()
    {
        RoutedEventArgs e = new RoutedEventArgs(PDataGrid.SortClickEvent);
        RaiseEvent(e);
    }

    private static ICommand sortClick;
    public static ICommand SortClick
    {
        get
        {
            if (sortClick == null)
            {
                sortClick = new BaseCommand(sort);
            }

            return sortClick;
        }
        set { sortClick = value; }
    }

    private static void sort()
    {
        // sort() = static, therefore not working...
        //raiseSortClickEvent();
    }