binding - MVVM DataGrid SelectedItem 绑定(bind)未更新

标签 binding mvvm .net-4.0 wpfdatagrid selecteditem

我是 .Net 和 C# 的新手,对绑定(bind)有点困惑。我有实现 MVVM 并显示 DataGrid 的应用程序。我想要实现的是,当用户按下某个组合键时,当前选定单元格的内容会被复制到下面行中的单元格中。
我尝试将 DataGrid 的 SelectedItem 绑定(bind)到 ViewModel 属性,但它永远不会更新。 CommandParameter 也不起作用,项目计数始终为 0。
所以我无法提取用户选择的单元格,也无法读取所选单元格的内容。
有没有人建议如何解决这个问题或实现这个功能?
提前致谢。
代码:
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>
</DataGrid>

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=单元格不适用于 SelectedItem

这是正常的 mvvm 方式:

第一个要在每一行中显示的对象类型

 public class MyObject {}

第二个包含集合和所选项目的 View 模型
 public class MyViewmodel
 {
    public ObservableCollection<MyObject> MyItems {get;set;}
    public MyObject MySelectedItem {get;set;}

 }

xml 数据网格
 <DataGrid ItemsSource="{Binding MyItems}" SelectedItem="{Binding MySelectedItem, Mode=TwoWay}"/>

所以这就是你所拥有的。如果您现在想创建一个“复制”行的命令,您可以创建一个新对象 MyObject 并从您的 MySelectedItem 复制值,然后将新的 MyObject 添加到您的集合中。

但也许我不明白你的问题。

关于binding - MVVM DataGrid SelectedItem 绑定(bind)未更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11504934/

相关文章:

c# - string.Join 抛出异常

.net - 如何正确访问 C++/CLI initonly TimeSpan 字段?

binding - WSDL 外部 XSD 的 JAXB 绑定(bind)

binding - 如何在 knockout.js 中进行条件绑定(bind)?

c# - WPF 中的任务锁定 UI

wpf - 用于转换为 MVVM 模式的事件名称

c# - 是否有可能以 mvvm 模式在 wpf datagrid 上获取动态列?

wpf - ListBox项内的按钮时如何绑定(bind)命令

wpf - WPF 转换器可以访问它所绑定(bind)的控件吗?

c# - 当无法序列化枚举时,尽早失败或明确抛出