【问题标题】:Scrolling in Datagrid在 Datagrid 中滚动
【发布时间】:2012-09-14 17:08:41
【问题描述】:

我正在使用 MVVM。我使用一些代码将数据网格绑定到集合:

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

【问题讨论】:

    标签: wpf mvvm datagrid


    【解决方案1】:

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

    【讨论】:

    • 如果我将 EnableRowVirtualization 设置为 false,那么我的数据网格已关闭,应用程序也无法正常工作 =(
    • 我找到了解决方案,将属性 VirtualizingStackPanel.VirtualizationMode 设置为“标准”值!
    猜你喜欢
    • 2017-07-12
    • 1970-01-01
    • 2014-06-24
    • 2011-10-06
    • 2011-11-05
    • 2017-02-06
    • 2012-08-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多