简体   繁体   English

WinUI DataGrid(来自 CommunityToolkit):如何获取当前选定的单元格?

[英]WinUI DataGrid (from CommunityToolkit): How to get the currently selected cell?

in a WinUI 3 application, I am using the DataGrid from the CommunityToolkit.WinUI.UI.Controls namespace, see MSDN DataGrid class .在 WinUI 3 应用程序中,我使用来自CommunityToolkit.WinUI.UI.Controls命名空间的DataGrid ,请参阅MSDN DataGrid 类

I am looking for a way the get the instance of the currently selected or clicked DataGridCell .我正在寻找一种获取当前选定或单击的DataGridCell实例的方法。

I need this because I would like to show a MenuFlyout when the user selects / clicks a certain cell (The MenuFlyout control is like a little popup window, see MSDN MenuFlyout ).我需要这个,因为我想在用户选择/单击某个单元格时显示MenuFlyoutMenuFlyout控件就像一个小弹出窗口,请参阅MSDN MenuFlyout )。 This flyout should than be displayed next to cell.此弹出窗口应显示在单元格旁边。 For this, the MenuFlyout class has a ShowAt method available which accepts a parameter of type FrameworkElement ("The element to use as the flyout's placement target.").为此, MenuFlyout类有一个ShowAt方法可用,它接受FrameworkElement类型的参数(“ ShowAt放置目标的元素。”)。 I want to pass the currently selected cell to this method.我想将当前选定的单元格传递给这个方法。

This is my current code:这是我当前的代码:

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

The problem is that I do not know how to get an instance of the currently selected / clicked cell of the DataGrid.问题是我不知道如何获取 DataGrid 当前选定/单击的单元格的实例。 I could not find anything in the DataGrid class which I found helpful for that.我在DataGrid类中找不到任何对此有帮助的内容。 Unfortunatly the CurrentCellChanged event which I use in the code example above does not have this information in its event arguments either, it just has a parameter e of type EventArgs .不幸的是,我在上面的代码示例中使用的CurrentCellChanged事件在其事件参数中也没有此信息,它只有一个EventArgs类型的参数e The sender in that case is the DataGrid, not the cell.在这种情况下, sender是 DataGrid,而不是单元格。

What I did find is that there is a CurrentColumn property on the DataGrid, see MSDN DataGrid CurrentColumn .我确实发现 DataGrid 上有一个 CurrentColumn 属性,请参阅MSDN DataGrid CurrentColumn But I was not able to get from this instance to the DataGridCell instances of the row.但是我无法从这个实例到该行的DataGridCell实例。

Does anyone know how to get an instance of the currently selected or clicked DataGridCell ?有谁知道如何获取当前选择或点击的DataGridCell的实例?

I found an acceptable solution to my problem, but it does not involve getting the instance of the selected DataGridCell .我为我的问题找到了一个可接受的解决方案,但它不涉及获取所选DataGridCell的实例。

What I do in order to work with the element of a cell is changing the respective column of the DataGrid to a column of type DataGridTemplateColumn .我做的,以与小区的单元工作进行改变的各个栏DataGrid到类型的列DataGridTemplateColumn This allows defining the controls which make up each cell.这允许定义组成每个单元格的控件。 For instance using a Button or DropDownButton as cell content.例如使用ButtonDropDownButton作为单元格内容。

The following code declaratively creates a DataGridTemplateColumn which renders the content of the property Text of class ViewModelForRowData using a DropDownButton .以下代码声明性地创建了一个DataGridTemplateColumn ,它使用DropDownButton呈现类ViewModelForRowData的属性Text的内容。 The DropDownButton displays MenuFlyout next to the button (see my question). DropDownButton在按钮旁边显示MenuFlyout (请参阅我的问题)。 The dropdwon has three options (menu items) that will appear if the user clicks on the DropDownButton that represents the cell content. dropdwon 具有三个选项(菜单项),如果用户单击代表单元格内容的DropDownButton ,则会出现这些选项。 Because the DropDownButton actually is the cell content, this is effectively that what I need, ie I do not need the clicked DataGridCell instance itself:因为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>

An alternative would be creating the MenuFlyout in a code behind file, that gets called as a result of a button click event handler.另一种方法是在代码隐藏文件中创建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);
    }

In that case the sender parameter of the event handler references the button, so i can use this to specify the first parameter of the MenuFlyout.ShowAt method.在这种情况下,事件处理程序的sender参数引用按钮,因此我可以使用它来指定MenuFlyout.ShowAt方法的第一个参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM