Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何获取绑定到可观察集合的datagrid,以使用mvvm通知删除?_C#_Wpf_Xaml_Mvvm - Fatal编程技术网

C# 如何获取绑定到可观察集合的datagrid,以使用mvvm通知删除?

C# 如何获取绑定到可观察集合的datagrid,以使用mvvm通知删除?,c#,wpf,xaml,mvvm,C#,Wpf,Xaml,Mvvm,我有一个绑定到可观察集合的datagrid。我想知道何时从datagrid中删除一行(或多行)。我正在尝试使用mvvm来实现这一点 我不担心属性的改变(它都是只读的)只是删除。所以我知道我只需要使用CollectionChanged事件。然而,我不知道我如何将其连接起来,尤其是使用mvvm 数据网格 <DataGrid Grid.Row="0" ItemsSource="{Binding BookList, UpdateSourceTrigger=PropertyCha

我有一个绑定到可观察集合的datagrid。我想知道何时从datagrid中删除一行(或多行)。我正在尝试使用mvvm来实现这一点

我不担心属性的改变(它都是只读的)只是删除。所以我知道我只需要使用CollectionChanged事件。然而,我不知道我如何将其连接起来,尤其是使用mvvm

数据网格

<DataGrid Grid.Row="0"
          ItemsSource="{Binding BookList, UpdateSourceTrigger=PropertyChanged}"
          Style="{StaticResource DataGridTemplate1}"
          ColumnHeaderStyle="{StaticResource DG_ColumnHeaderCenter1}"                                            
          RowStyle="{StaticResource DG_Row1}"
          CellStyle="{StaticResource DG_Cell1}"                                    
          RowHeaderStyle="{StaticResource DG_RowHeader1}"
          AutoGenerateColumns="False"
          HorizontalAlignment="Stretch" 
          CanUserDeleteRows="True"                          
          Background="Silver"
          RowHeaderWidth="30">
    <DataGrid.Columns>
        <DataGridTextColumn Header="DatePrice" IsReadOnly="True" Binding="{Binding DatePrice, StringFormat={}\{0:dd-MMM-yy\}}" MinWidth="75"/>
        <DataGridTextColumn Header="ISIN" IsReadOnly="True" Binding="{Binding ISIN}" MinWidth="75"/>
        <DataGridTextColumn Header="Name" IsReadOnly="True" Binding="{Binding Name}" MinWidth="75"/>
        <DataGridTextColumn Header="Price" IsReadOnly="True" Binding="{Binding Price, StringFormat={}{0:N0}}" MinWidth="75"/>                           
    </DataGrid.Columns>
</DataGrid>

你可以写这样的东西。添加、删除、更改、移动项目或刷新整个列表时,会发生此事件

BookList.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler( BookList_CollectionChanged );

void BookList_CollectionChanged( object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e )
{
    if ( e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Remove )
    {

    }
}

你可以这样写。添加、删除、更改、移动项目或刷新整个列表时,会发生此事件

BookList.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler( BookList_CollectionChanged );

void BookList_CollectionChanged( object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e )
{
    if ( e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Remove )
    {

    }
}

实现INotifyPropertyChanged

用这个

  public event PropertyChangedEventHandler PropertyChanged;

    private void OnPropertyChanged(string property)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(property));
    }
然后在集合的(ItemsSource of DataGrid)Setter中,添加

OnPropertyChanged("CollectionName");

实现INotifyPropertyChanged

用这个

  public event PropertyChangedEventHandler PropertyChanged;

    private void OnPropertyChanged(string property)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(property));
    }
然后在集合的(ItemsSource of DataGrid)Setter中,添加

OnPropertyChanged("CollectionName");

谢谢事件正在发生。但是,我如何知道哪个删除已被删除或正在被删除?@mHelpMe查看
NotifyCollectionChangedEventArgs
事件参数的
OldItems
属性。还有,谢谢。我还看到如果他们删除多行,即3行,那么事件将被称为3次感谢。事件正在发生。但是,我如何知道哪个删除已被删除或正在被删除?@mHelpMe查看
NotifyCollectionChangedEventArgs
事件参数的
OldItems
属性。还有,谢谢。我还看到如果他们删除多行,即3行,事件将被调用3次