如何从WPF KeyBinding中的DataGrid传递单元格信息? [英] How to Pass Cell Information from DataGrid in WPF KeyBinding?

查看:166
本文介绍了如何从WPF KeyBinding中的DataGrid传递单元格信息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个DataGrid列出MobileInfo集合。 DataGrid配置为 SelectionUnit =FullRow。如果我单击任何行,那么它会选择整个行,另外它将鼠标指向鼠标的边框。边框选择在键盘导航时移动例如:向上 Down 。基于单元格选择,我希望传递有关单元格的信息。



请参阅图像具有输出屏幕





在上述屏幕截图中,Android被选中,基于键盘导航,单元格选择获取



我的XAML源代码:

 < DataGrid AutoGenerateColumns =FalseItemsSource ={Binding MobileList,UpdateSourceTrigger = PropertyChanged}SelectionUnit =FullRowIsReadOnly =True> 
< DataGrid.InputBindings>
< KeyBinding Key =C修饰符=CtrlCommand ={Binding Path = DataContext.CopyToClipBoardCommand}CommandParameter ={Binding}/>
< /DataGrid.InputBindings>
< DataGrid.Columns>
<! - 第1列 - >
< DataGridTextColumn Binding ={Binding MobileName}Header =Name/>
<! - 第2列 - >
< DataGridTextColumn Binding ={Binding MobileOS}Header =OS/>
< /DataGrid.Columns>
< / DataGrid>




注意:不要更改 SelectionUnit 在DataGrid中


请提供您的解决方案,如何传递基于键盘导航的单元格信息



与XAML DataGrid相关联的C#源代码

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

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;组; }
public string MobileOS {get;组; }
}


解决方案

你可以绑定命令参数 DataGrid.CurrentCell 属性。有几种方法可以实现,其中之一是指定绑定的相对来源:

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

请注意,我删除了 DataContext。从命令绑定路径( DataContext 是未明确指定源代码的绑定的默认源)。



命令参数现在将是一个类型为 DataGridCellInfo ,这是一个结构,而不是一个类。



您可以修改命令参数绑定路径以提取更具体的信息。


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

解决方案

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.

这篇关于如何从WPF KeyBinding中的DataGrid传递单元格信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆