【问题标题】:WinUI DataGrid (from CommunityToolkit): How to get the currently selected cell?WinUI DataGrid(来自 CommunityToolkit):如何获取当前选定的单元格?
【发布时间】:2021-12-19 11:27:34
【问题描述】:

在 WinUI 3 应用程序中,我使用来自 CommunityToolkit.WinUI.UI.Controls 命名空间的 DataGrid,请参阅 MSDN DataGrid class

我正在寻找一种方法来获取当前选择或点击的DataGridCell 的实例。

我需要这个,因为我想在用户选择/单击某个单元格时显示MenuFlyoutMenuFlyout 控件就像一个小弹出窗口,请参阅MSDN MenuFlyout)。此弹出按钮应显示在单元格旁边。为此,MenuFlyout 类有一个可用的ShowAt 方法,它接受FrameworkElement 类型的参数(“用作浮出控件放置目标的元素。”)。我想将当前选中的单元格传递给这个方法。

这是我当前的代码:

    private void MyDataGrid_CurrentCellChanged(object sender, System.EventArgs e)
    {
        MenuFlyout flyout = new MenuFlyout();
        MenuFlyoutItem item = new MenuFlyoutItem();
        item.Text = "Test";
        flyout.Items.Add(item);

        // How do I get an instance of the currently selected cell in the DataGrid?
        FrameworkElement theCurrentlySelectedCell = ?;
        flyout.ShowAt(theCurrentlySelectedCell);
    }

问题是我不知道如何获取 DataGrid 当前选择/单击的单元格的实例。我在DataGrid 类中找不到任何对我有帮助的东西。 不幸的是,我在上面的代码示例中使用的CurrentCellChanged 事件在其事件参数中也没有此信息,它只有一个e 类型的EventArgs 参数。在这种情况下,sender 是 DataGrid,而不是单元格。

我发现DataGrid 上有一个CurrentColumn 属性,请参阅MSDN DataGrid CurrentColumn。但我无法从这个实例到行的DataGridCell 实例。

有谁知道如何获取当前选中或点击的DataGridCell的实例?

【问题讨论】:

  • 您检查过 SelectionChanged 事件吗?也许它更适合您的需求,因为它提供了一个 SelectionChangedEventArgs。
  • 不幸的是,这对我没有帮助。 SelectionChangedEventArgs 包含一个“AddedItems”属性,但这仅包含绑定到每一行的数据对象的实例,或者更准确地说是绑定到现在选择的行。 “OriginalSource”属性始终为空。

标签: c# user-interface windows-community-toolkit winui-3


【解决方案1】:

我为我的问题找到了一个可接受的解决方案,但它不涉及获取所选DataGridCell 的实例。

为了使用单元格的元素,我所做的是将DataGrid 的相应列更改为DataGridTemplateColumn 类型的列。 这允许定义构成每个单元格的控件。例如使用ButtonDropDownButton 作为单元格内容。

以下代码以声明方式创建一个DataGridTemplateColumn,它使用DropDownButton 呈现类ViewModelForRowData 的属性Text 的内容。 DropDownButton 在按钮旁边显示MenuFlyout(请参阅我的问题)。如果用户单击代表单元格内容的DropDownButton,dropdwon 将出现三个选项(菜单项)。因为DropDownButton 实际上是单元格内容,所以这实际上就是我需要的,即我不需要点击的DataGridCell 实例本身:

            <controls:DataGridTemplateColumn Header="Test">
                <controls:DataGridTemplateColumn.CellTemplate>
                    <DataTemplate x:DataType="viewModels:ViewModelForRowData">
                        <DropDownButton Content="{x:Bind Text}" HorizontalAlignment="Stretch" >
                            <DropDownButton.Flyout>
                                <MenuFlyout Placement="Bottom" ShowMode="Transient">
                                    <MenuFlyoutItem Text="Menu Option 1" Command="{x:Bind Command1}" />
                                    <MenuFlyoutItem Text="Menu Option 2" Command="{x:Bind Command2}" />   
                                    <MenuFlyoutItem Text="Menu Option 3" Command="{x:Bind Command3}" />   
                                </MenuFlyout>
                            </DropDownButton.Flyout>
                        </DropDownButton>
                    </DataTemplate>
                </controls:DataGridTemplateColumn.CellTemplate>
            </controls:DataGridTemplateColumn>

另一种方法是在代码隐藏文件中创建MenuFlyout,该文件因按钮单击事件处理程序而被调用。

            <controls:DataGridTemplateColumn Header="Test" >
                <controls:DataGridTemplateColumn.CellTemplate>
                    <DataTemplate x:DataType="viewModels:ViewModelForRowData">
                        <Button Content="{x:Bind Text}" Click="ButtonClick"></Button>
                    </DataTemplate>
                </controls:DataGridTemplateColumn.CellTemplate>
            </controls:DataGridTemplateColumn>



    private void ButtonClick(object sender, Microsoft.UI.Xaml.RoutedEventArgs e)
    {
        Button button = sender as Button;

        if (button == null)
        {
            return;
        }

        MenuFlyout flyout = new MenuFlyout();

        MenuFlyoutItem item1 = new MenuFlyoutItem();
        item1.Text = "TestItem 1";
        flyout.Items.Add(item1);

        MenuFlyoutItem item2 = new MenuFlyoutItem();
        item2.Text = "TestItem 2";
        flyout.Items.Add(item2);

        FlyoutShowOptions flyoutShowOptions = new FlyoutShowOptions();
        flyout.ShowAt(button, flyoutShowOptions);
    }

在这种情况下,事件处理程序的sender 参数引用了按钮,所以我可以使用它来指定MenuFlyout.ShowAt 方法的第一个参数。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-01-29
    • 1970-01-01
    • 2016-11-22
    • 2023-03-06
    • 2019-01-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多