![](/img/trans.png)
[英]Get currently selected cell on Enter pressed in a datagrid C# WPF
[英]WinUI DataGrid (from CommunityToolkit): How to get the currently selected cell?
在 WinUI 3 應用程序中,我使用來自CommunityToolkit.WinUI.UI.Controls
命名空間的DataGrid
,請參閱MSDN DataGrid 類。
我正在尋找一種獲取當前選定或單擊的DataGridCell
實例的方法。
我需要這個,因為我想在用戶選擇/單擊某個單元格時顯示MenuFlyout
( MenuFlyout
控件就像一個小彈出窗口,請參閱MSDN MenuFlyout )。 此彈出窗口應顯示在單元格旁邊。 為此, MenuFlyout
類有一個ShowAt
方法可用,它接受FrameworkElement
類型的參數(“ ShowAt
放置目標的元素。”)。 我想將當前選定的單元格傳遞給這個方法。
這是我當前的代碼:
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
事件在其事件參數中也沒有此信息,它只有一個EventArgs
類型的參數e
。 在這種情況下, sender
是 DataGrid,而不是單元格。
我確實發現 DataGrid 上有一個 CurrentColumn 屬性,請參閱MSDN DataGrid CurrentColumn 。 但是我無法從這個實例到該行的DataGridCell
實例。
有誰知道如何獲取當前選擇或點擊的DataGridCell
的實例?
我為我的問題找到了一個可接受的解決方案,但它不涉及獲取所選DataGridCell
的實例。
我做的,以與小區的單元工作進行改變的各個欄DataGrid
到類型的列DataGridTemplateColumn
。 這允許定義組成每個單元格的控件。 例如使用Button
或DropDownButton
作為單元格內容。
以下代碼聲明性地創建了一個DataGridTemplateColumn
,它使用DropDownButton
呈現類ViewModelForRowData
的屬性Text
的內容。 DropDownButton
在按鈕旁邊顯示MenuFlyout
(請參閱我的問題)。 dropdwon 具有三個選項(菜單項),如果用戶單擊代表單元格內容的DropDownButton
,則會出現這些選項。 因為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
方法的第一個參數。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.