MVVM的DataGrid从选定的单元格复制信息 [英] MVVM DataGrid copy information from selected cells
问题描述
我使用的是.NET 4.0中的DataGrid MVVM模式。我需要让用户选择单元格,并从选定的单元格中的信息复制到其他的DataGrid行(通过键盘快捷键或上下文菜单中的复制/粘贴)。我试图通过的SelectedItem或发送的SelectedItem作为CommandParameter但这功能只与整排,而不是与细胞来实现这一点。 (DataGrid绑定到的ObservableCollection包含的对象与浮点字段,这些字段将被映射到数据网格细胞) 那么,有没有在WPF MVVM任何解决方案如何从一个行复制数据网格单元格到另一个? 提前致谢 XAML:
<数据网格Grid.Row =1Grid.Column =0Grid.ColumnSpan =9的AutoGenerateColumns =FALSE高度=自动的HorizontalAlignment =左
NAME =dataGrid的VerticalAlignment =热门WIDTH ={结合的ElementName = GRID4,路径=宽度}
ScrollViewer.CanContentScroll =假FrozenColumnCount =1SelectionUnit =细胞的SelectionMode =扩展CanUserSortColumns =假
CanUserReorderColumns =FALSECanUserResizeRows =假的rowHeight =25RowBackground =浅灰色AlternatingRowBackground =白
ScrollViewer.VerticalScrollBarVisibility =可见ScrollViewer.HorizontalScrollBarVisibility =可见
的ItemsSource ={结合层,模式=双向}的SelectedItem ={绑定的RelativeSource = {的RelativeSource FindAncestor,AncestorType = {X:类型窗口}},路径= DataContext.Selection,模式=双向}>
< DataGrid.InputBindings>
<按键绑定手势=移位命令={结合ItemHandler}CommandParameter ={结合的ElementName = DataGrid后,路径= SelectedItems}>< /按键绑定>
< /DataGrid.InputBindings>
视图模型:
专用浮_selection = 0.0;
公众持股量的选择
{
得到
{
返回_selection;
}
组
{
如果(_selection!=值)
{
_selection =价值;
NotifyPropertyChanged(选择);
}
}
}
...
公共DelegateCommand< IList的> SelectionChangedCommand =新DelegateCommand< IList的>(
项目=>
{
如果(项目== NULL)
{
NumberOfItemsSelected = 0;
返回;
}
NumberOfItemsSelected = items.Count;
});
公众的ICommand ItemHandler
{
得到
{
返回SelectionChangedCommand;
}
}
我想你可能是 SelectionUnit
属性后面。将其设置为 CellOrRowHeader
从改变整行选择方式为一个单元格。
你失去了漂亮的什么行叫什么名字?突出但你正专注于单个细胞。 (你也许可以扩展数据网格与当前行的逻辑添加您自己的亮点。)
<数据网格的AutoGenerateColumns =真Grid.Column =1Grid.Row =0
的HorizontalAlignment =拉伸NAME =dataGrid的VerticalAlignment =拉伸
的DataContext ={结合}的ItemsSource ={绑定路径= MyDataTable}
的IsReadOnly =真的SelectionMode =扩展SelectionUnit =CellOrRowHeader/>
I’m using .Net 4.0 DataGrid with MVVM pattern. I need to enable user to select cells and copy information from selected cells to other DataGrid rows (either via keyboard shortcut or context-menu copy/paste). I tried to achieve this via SelectedItem or sending SelectedItem as CommandParameter but this functions only with the whole row and not with cells. (DataGrid is bound to ObservableCollection that contains objects with float fields. These fields are then mapped to DataGrid cells) So, is there any solution in WPF MVVM how to copy DataGrid cells from one row to another? Thanks in advance xaml:
<DataGrid Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="9" AutoGenerateColumns="False" Height="Auto" HorizontalAlignment="Left"
Name="dataGrid" VerticalAlignment="Top" Width="{Binding ElementName=grid4,Path=Width}"
ScrollViewer.CanContentScroll="False" FrozenColumnCount="1" SelectionUnit="Cell" SelectionMode="Extended" CanUserSortColumns = "False"
CanUserReorderColumns="False" CanUserResizeRows="False" RowHeight="25" RowBackground="LightGray" AlternatingRowBackground="White"
ScrollViewer.VerticalScrollBarVisibility="Visible" ScrollViewer.HorizontalScrollBarVisibility="Visible"
ItemsSource="{Binding Layers, Mode=TwoWay}" SelectedItem="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=DataContext.Selection, Mode=TwoWay}">
<DataGrid.InputBindings>
<KeyBinding Gesture="Shift" Command="{Binding ItemHandler}" CommandParameter="{Binding ElementName=dataGrid, Path=SelectedItems}"></KeyBinding>
</DataGrid.InputBindings>
ViewModel:
private float _selection = 0.0f;
public float Selection
{
get
{
return _selection;
}
set
{
if (_selection != value)
{
_selection = value;
NotifyPropertyChanged("Selection");
}
}
}
...
public DelegateCommand<IList> SelectionChangedCommand = new DelegateCommand<IList>(
items =>
{
if (items == null)
{
NumberOfItemsSelected = 0;
return;
}
NumberOfItemsSelected = items.Count;
});
public ICommand ItemHandler
{
get
{
return SelectionChangedCommand;
}
}
I think you might be after the SelectionUnit
property. Setting this to CellOrRowHeader
changes the selection method from full row to a single cell.
You do lose the nice "what row am I on?" highlighting but you are focusing on a single cell. (You could probably extend the datagrid to add your own highlight with the current row logic.)
<DataGrid AutoGenerateColumns="True" Grid.Column="1" Grid.Row="0"
HorizontalAlignment="Stretch" Name="dataGrid" VerticalAlignment="Stretch"
DataContext="{Binding}" ItemsSource="{Binding Path=MyDataTable}"
IsReadOnly="True" SelectionMode="Extended" SelectionUnit="CellOrRowHeader" />
这篇关于MVVM的DataGrid从选定的单元格复制信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!