How to Pass Cell Information from DataGrid in WPF

2019-03-05 06:25发布

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

Cell Selected in OS Column

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

3条回答
何必那么认真
2楼-- · 2019-03-05 06:57

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.

查看更多
趁早两清
3楼-- · 2019-03-05 06:57

This code might help you in what you want to achieve by MVVM :

<i:Interaction.Triggers>
    <i:EventTrigger  EventName="KeyUp">
        <i:InvokeCommandAction Command="{Binding Path=DataContext.CurrentCellChangeCommand,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type Window}}}"
                                    CommandParameter="{Binding CurrentCell, RelativeSource={RelativeSource FindAncestor, AncestorType=DataGrid}}"
                                    ></i:InvokeCommandAction>
    </i:EventTrigger>
</i:Interaction.Triggers>

ViewModel Code,

public ICommand CurrentCellChangeCommand
{
    get { return new RelayCommand(CurrentCellChangeExecute); }
}

private void CurrentCellChangeExecute(object obj)
{
    var dataGridCellInfo = (System.Windows.Controls.DataGridCellInfo)obj;

    var mobileInfo = dataGridCellInfo.Item as MobileInfo;

    if (mobileInfo != null)
    {
        if (dataGridCellInfo.Column.Header.ToString() == "Name")
        {
            SelectedCellValue = mobileInfo.MobileName;
            //mobileInfo.MobileName 
        }
        else if (dataGridCellInfo.Column.Header.ToString() == "OS")
        {
            SelectedCellValue = mobileInfo.MobileOS;
            //mobileInfo.MobileOS 
        }
    }
}
查看更多
相关推荐>>
4楼-- · 2019-03-05 07:07

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;
        }
查看更多
登录 后发表回答