在Datagrid中滚动

时间:2012-09-14 17:08:42

标签: wpf mvvm datagrid

我正在使用MVVM。我使用一些代码将datagrid绑定到集合:

<commonMVVMControls:GridControl DataContext="{Binding Path=ClientsListGrid,
                                                          Mode=TwoWay}"

这是DataGridControl类:

public class GridControl : DataGrid
{
    public GridControl()
    {
        this.DataContextChanged += new System.Windows.DependencyPropertyChangedEventHandler(GridControl_DataContextChanged);
    }

    void GridControl_DataContextChanged(object sender, System.Windows.DependencyPropertyChangedEventArgs e)
    {
        var bindingItemsSource = new Binding("ItemsSource");
        bindingItemsSource.Source = this.DataContext;
        this.SetBinding(DataGrid.ItemsSourceProperty, bindingItemsSource);

        this.RowStyle = new Style(typeof(DataGridRow));
        this.RowStyle.Setters.Add(new Setter(DataGridRow.IsSelectedProperty, new Binding("IsSelected")));
    }

现在是ViewModel中的一段代码:

var selectedClient = this.ClientsListGrid.ItemsSource.Where(x => x.IsSelected);
        if (!selectedClient.Any()) 
        {
            MessageBox.Show(Resource.Resource.UpdateUserError, Resource.Resource.Warning, MessageBoxButton.OK, MessageBoxImage.Stop,
            MessageBoxResult.OK);
            return;
        }

        var viewModel = new AddOrUpdateClientViewModel(_serviceContext, selectedClient.First()); 

效果很好。但是,如果我向下或向上滚动数据网格,它将停止工作,并且IsSelected始终等于false。

1 个答案:

答案 0 :(得分:0)

尝试查看禁用虚拟化会发生什么。它可能与此有关。