【问题标题】:WPF Customcontrol based on DataGrid + Custom Events基于 DataGrid + 自定义事件的 WPF 自定义控件
【发布时间】:2012-07-20 16:20:23
【问题描述】:

我目前正在编写基于标准 WPF Datagrid 的自定义控件。我已经实现了一个带有排序、分组和过滤等功能的工具栏。使用 ICollectionView 作为 ItemsSource 这些功能相当容易实现。

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

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

但是我如何在我的视图之外引发这些事件,以便任何使用我的控件的类(在我的情况下为 MyDataGridView.cs)都可以处理它们?

我的 ICommand 被定义为静态的(正如我在一些示例中看到的那样)。 我将使用 RaiseEvent 来引发我的视图模型可以捕获的 RoutedEvent 是非静态的。

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

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


哦,我忘了提到我正在 MVVM 模式下开发我的控件,并希望坚持下去。整个逻辑(过滤、分组、排序)应该在我的视图模型中。

编辑: 哦,我忘了提到我正在 MVVM 模式下开发我的控件,并希望坚持下去。整个逻辑(过滤、分组、排序)应该在我的视图模型中。

【问题讨论】:

    标签: wpf events datagrid custom-controls


    【解决方案1】:

    我没有完全理解。是什么阻止您将按钮命令绑定到 viewmodel 并在 viewmodel 中发挥作用? (如 CollectionView 排序等)。通常你直接绑定 DataContext&viewmodel。不涉及任何代码。

    【讨论】:

    • 嗯,我很难解释它 - 我的英语有点生疏 - 但我会尝试。一方面,我有一个 CustomDataGrid,其中添加了按钮 = 'Control-View'。在另一边有我的 MainWindow = View,它绑定到一个 ViewModel。我的理解是,我需要将过滤、排序和分组的所有逻辑放在我的 Control-View 中。
    • 但是这个“Control-View”无法访问 ItemSource,它位于我的 MainWindow 的 ViewModel 中。我希望我的问题现在更清楚了-如果不是,请不要犹豫!我会很感激任何帮助
    • 现在在您的 ViewModel 中,实现 SortClick 命令并在那里,实现逻辑,像这样:YourCollection.Sort();也就是说,假设您的控件是可视化树和 MainWindow 的一部分。
    • 有了这个,我将不得不在它之外执行属于我的 CustomDataGrid 的所有逻辑。每个使用该控件的人不仅要处理 ICollectionView.Sort 等,还需要为每个函数编写一个窗口来配置它(例如,sort-config 的窗口由一个数据网格组成,您可以在其中选择列和排序方向)。所以不幸的是,将事件扔给我的 MainWindow 的 ViewModel 对我没有帮助。 ://
    • 将 SortClickCommand 添加到 CustomDataGri。现在将新的事件处理程序添加到 CustomDataGrid.cs,例如 SortButtonClicked,并在其中执行 SortClickCommand.Execute()。这让每个人都可以随心所欲地覆盖 SortClickCommand 的功能。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-23
    • 2020-10-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多