How to Pass Cell Information from DataGrid in WPF KeyBinding?

拈花ヽ惹草 提交于 2019-12-04 06:18:18

问题


I'm having a DataGrid to list the MobileInfo Collection. The DataGrid is Configured with SelectionUnit="FullRow". If I Click the any Row then it selects the entire row with additionally it points the Cell with border where the Mouse was hit. The Border Selection moves while on Keyboard navigation For Example : Left, Right, Up and Down. Based on the Cell Selection I wish to Pass the Information about the Cell.

Refer the Image it has the Output Screen

In the above Screen Shot the Android is Selected, Based on Keyboard Navigation, the Cell selection gets changed.

My XAML Source Code:

<DataGrid  AutoGenerateColumns="False" ItemsSource="{Binding MobileList, UpdateSourceTrigger=PropertyChanged}" SelectionUnit="FullRow" IsReadOnly="True">
    <DataGrid.InputBindings>
        <KeyBinding Key="C" Modifiers="Ctrl" Command="{Binding Path=DataContext.CopyToClipBoardCommand}" CommandParameter="{Binding }" />
    </DataGrid.InputBindings>
    <DataGrid.Columns>
        <!--Column 1-->
        <DataGridTextColumn Binding="{Binding MobileName}" Header="Name" />
        <!--Column 2-->
        <DataGridTextColumn Binding="{Binding MobileOS}" Header="OS" />
    </DataGrid.Columns>
</DataGrid>

Note: Don't Change the SelectionUnit in the DataGrid

Kindly provide your solution, how to pass the Cell Information based on Keyboard Navigation

The C# Source Code associated with the XAML DataGrid

public class GridViewModel
{
    public ObservableCollection<MobileInfo> MobileList { get; set; }

    public GridViewModel()
    {
        MobileList = new ObservableCollection<MobileInfo>();
        MobileList.Add(new MobileInfo  { MobileName = "iPhone", MobileOS = "iOS" });
        MobileList.Add(new MobileInfo { MobileName = "Xperia", MobileOS = "Android" });
        MobileList.Add(new MobileInfo { MobileName = "Lumina", MobileOS = "Windows" });
    }

}

public class MobileInfo
{
    public string MobileName { get; set; }
    public string MobileOS { get; set; }
}

回答1:


You could bind the command parameter to DataGrid.CurrentCell property. There are several ways to accomplish that, one of which is specifying relative source for the binding:

<KeyBinding Key="C"
            Modifiers="Control"
            Command="{Binding CopyToClipBoardCommand}"
            CommandParameter="{Binding CurrentCell, RelativeSource={RelativeSource FindAncestor, AncestorType=DataGrid}}" />

Note that I removed the DataContext. part from the command binding path (DataContext is the default source for binding if source is not explicitly specified).

The command parameter will now be an object of type DataGridCellInfo, which is a structure, and not a class.

You can modify the command parameter binding path to extract more specific info.




回答2:


you can simply use CurrentCellChanged Event in DataGrid xaml.

 CurrentCellChanged="DataGrid_CurrentCellChanged"

code...

private void DataGrid_CurrentCellChanged(object sender, EventArgs e)
        {
            var grid = sender as DataGrid;
            var cell = grid.CurrentCell.Item;
        }


来源:https://stackoverflow.com/questions/34738460/how-to-pass-cell-information-from-datagrid-in-wpf-keybinding

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!