我是.Net和C#的新手,对绑定有点困惑.我有应用程序实现MVVM并显示DataGrid.我想要实现的是当用户按下某个键组合时,当前所选单元格的内容被复制到下面行中的单元格.我已经尝试将DataGrid的SelectedItem绑定到ViewModel属性,但它永远不会更新.CommandParameter也不起作用,项目计数始终为0.因此我无法提取用户选择的单元格,也无法读取所选单元格的内容.有没有人有如何解决这个问题或实现这个功能的建议?提前致谢.代码: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>
</DataGrid>
Run Code Online (Sandbox Code Playgroud)
视图模型:
private float _selection = 0.0f;
public float Selection
{
get
{
return _selection;
}
set
{
if (_selection != value)
{
_selection = value;
NotifyPropertyChanged("Selection");
}
}
}
Run Code Online (Sandbox Code Playgroud)
...
public DelegateCommand<IList> SelectionChangedCommand …Run Code Online (Sandbox Code Playgroud)