gpt4 book ai didi

MVVM DataGrid 从选定单元格复制信息

转载 作者:行者123 更新时间:2023-12-01 05:04:47 25 4
gpt4 key购买 nike

我正在使用带有 MVVM 模式的 .Net 4.0 DataGrid。我需要使用户能够选择单元格并将信息从选定的单元格复制到其他 DataGrid 行(通过键盘快捷键或上下文菜单复制/粘贴)。我试图通过 SelectedItem 或将 SelectedItem 作为 CommandParameter 发送来实现这一点,但这仅适用于整行而不适用于单元格。
(DataGrid 绑定(bind)到包含具有浮点字段的对象的 ObservableCollection。然后将这些字段映射到 DataGrid 单元格)
那么,WPF MVVM 中是否有任何解决方案如何将 DataGrid 单元格从一行复制到另一行?
提前致谢
xml:

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

View 模型:
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;
}
}

最佳答案

我想你可能会追求 SelectionUnit属性(property)。将此设置为 CellOrRowHeader将选择方法从整行更改为单个单元格。

你确实失去了“我在哪排?”突出显示,但您专注于单个单元格。 (您可能可以扩展数据网格以使用当前行逻辑添加您自己的突出显示。)

<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 从选定单元格复制信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11518453/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com