如何从DataGrid中转至WPF键绑定小区信息? [英] How to Pass Cell Information from DataGrid in WPF KeyBinding?

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

问题描述

我有一个DataGrid列出MobileInfo集合。 DataGrid中配置了 SelectionUnit =FullRow。如果我点击任何行则选择整个一行还指向与边界电池,其中鼠标被击中。而在键盘导航例如边框选择招式:最多。基于小区选择我要传递有关小区信息。



请参阅它具有输出屏幕上的图像





在拍摄了Android上面的屏幕中选择,基于键盘导航,小区选择得到改变



我的XAML源代码:

 <数据网格的AutoGenerateColumns = 假的ItemsSource ={结合MobileList,UpdateSourceTrigger =的PropertyChanged}SelectionUnit =FullRowIsReadOnly =真> 
< D​​ataGrid.InputBindings>
<键绑定键=C修饰符=CTRL命令={绑定路径= DataContext.CopyToClipBoardCommand}CommandParameter ={结合}/>
< /DataGrid.InputBindings>
< D​​ataGrid.Columns>
<! - 列1 - >
< D​​ataGridTextColumn绑定={结合MobileName}标题=名称/>
<! - 列2 - >
< D​​ataGridTextColumn绑定={结合MobileOS}标题=OS/>
< /DataGrid.Columns>
< / DataGrid的>




请注意:不要更改 SelectionUnit 在DataGrid




请提供您的解决方案,如何根据键盘导航通过小区信息



与XAML的DataGrid

 公共类GridViewModel 
{$ b相关联的C#源代码$ b公众的ObservableCollection< MobileInfo> MobileList {搞定;组; }

公共GridViewModel()
{
MobileList =新的ObservableCollection< MobileInfo>();
MobileList.Add(新MobileInfo {MobileName =iPhone,MobileOS =iOS版});
MobileList.Add(新MobileInfo {MobileName =的Xperia,MobileOS =机器人});
MobileList.Add(新MobileInfo {MobileName =露明纳,MobileOS =视窗});
}

}

公共类MobileInfo
{
公共字符串MobileName {搞定;组; }
公共字符串MobileOS {搞定;组; }
}


解决方案

您可以绑定命令参数 DataGrid.CurrentCell 财产。有几种方法来实现这个目标,其中一个是指定相对源的绑定:

 <键绑定键=C 
修饰符=控制
命令={结合CopyToClipBoardCommand}
CommandParameter ={结合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.

这篇关于如何从DataGrid中转至WPF键绑定小区信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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