WPF MVVM分页

时间:2016-12-28 07:25:49

标签: c# wpf mvvm pagination

我有分页视图的用户控件,它有自己的viewmodel。我在页面中添加了分页,其中datagrid也有一个单独的viewmodel。

我的问题是,每次在我的分页视图模型中完成命令时,如何更新我的页面视图模型中的ObservableCollection?

这是我的PagingControl.xaml

<StackPanel Width="Auto"
        Orientation="Horizontal">
        <Button                
            Margin="4,0"
            Content="&lt;&lt;"
            Command="{Binding FirstCommand}"/>
        <Button
            Margin="4,0"
            Content="&lt;"
            Command="{Binding PreviousCommand}"/>
        <StackPanel
            VerticalAlignment="Center"
            Orientation="Horizontal">
            <TextBlock
                Text="{Binding Start}"/>
            <TextBlock
                Text=" to "/>
            <TextBlock
                Text="{Binding End}"/>
            <TextBlock
                Text=" of "/>
            <TextBlock
                Text="{Binding TotalItems}"/>
        </StackPanel>
        <Button
            Margin="4,0"
            Content="&gt;"
            Command="{Binding NextCommand}"/>
        <Button
            Margin="4,0"
            Content="&gt;&gt;"
            Command="{Binding LastCommand}"/>
        <ComboBox Width="100" ItemsSource="{Binding ItemsPerPage}" SelectedValue="{Binding ItemCount}">
            <i:Interaction.Triggers>
                <i:EventTrigger EventName="SelectionChanged">
                <i:InvokeCommandAction Command="{Binding CountChangedCommand}"/>
                </i:EventTrigger>
            </i:Interaction.Triggers>
        </ComboBox>
    </StackPanel>

PagingViewModel.cs

public class PagingViewModel : ViewModelBase
{
    private ObservableCollection<DataModel> _data;

    private int start = 0;
    private int itemCount = 10;
    private int totalItems = 0;
    private readonly List<int> count;

    private ICommand _firstCommand;
    private ICommand _previousCommand;
    private ICommand _nextCommand;
    private ICommand _lastCommand;
    private ICommand _countchangedCommand;

    public ObservableCollection<DataModel> Data
    {
        get { return _data; }
        set
        {
            if (_data!= value)
            {
                _data= value;
                OnPropertyChanged("Data");
            }
        }
    }

    public PagingViewModel()
    {
        count = new List<int> { 10, 20, 30};
        RefreshData();
    }

    public int Start { get { return start + 1; } }

    public int End { get { return start + itemCount < totalItems ? start + itemCount : totalItems; } }

    public int TotalItems { get { return totalItems; } }

    public List<int> Count { get { return count; } }

    public int ItemCount { get { return itemCount; } set { itemCount = value; OnPropertyChanged("ItemCount"); } }

    public ICommand FirstCommand
    {
        get
        {
            if (_firstCommand == null)
            {
                _firstCommand = new RelayCommand
                (
                    param =>
                    {
                        start = 0;
                        RefreshData();
                    },
                    param =>
                    {
                        return start - itemCount >= 0 ? true : false;
                    }
                );
            }

            return _firstCommand;
        }
    }

    public ICommand PreviousCommand
    {
        get
        {
            if (_previousCommand == null)
            {
                _previousCommand = new RelayCommand
                (
                    param =>
                    {
                        start -= itemCount;
                        RefreshData();
                    },
                    param =>
                    {
                        return start - itemCount >= 0 ? true : false;
                    }
                );
            }

            return _previousCommand;
        }
    }

    public ICommand NextCommand
    {
        get
        {
            if (_nextCommand == null)
            {
                _nextCommand = new RelayCommand
                (
                    param =>
                    {
                        start += itemCount;
                        RefreshData();
                    },
                    param =>
                    {
                        return start + itemCount < totalItems ? true : false;
                    }
                );
            }

            return _nextCommand;
        }
    }

    public ICommand LastCommand
    {
        get
        {
            if (_lastCommand == null)
            {
                _lastCommand = new RelayCommand
                (
                    param =>
                    {
                        start = (totalItems / itemCount - 1) * itemCount;
                        start += totalItems % itemCount == 0 ? 0 : itemCount;
                        RefreshData();
                    },
                    param =>
                    {
                        return start + itemCount < totalItems ? true : false;
                    }
                );
            }

            return _lastCommand;
        }
    }

    public ICommand CountChangedCommand
    {
        get
        {
            if (_countchangedCommand == null)
            {
                _countchangedCommand = new RelayCommand
                (
                    param =>
                    {
                        start = 0;
                        RefreshData();
                    },
                    param =>
                    {
                        return ((totalItems - itemCount) > -10) ? true : false;
                    }
                );
            }

            return _countchangedCommand;
        }
    }

    public void RefreshData()
    {
        _data= GetData(start, itemCount, out totalItems);
DataViewModel vm = new DataViewModel(this);

        OnPropertyChanged("Start");
        OnPropertyChanged("End");
        OnPropertyChanged("TotalItems");
    }
}

这里是我的Page的视图模型:DataViewModel.cs

public class DataViewModel: ViewModelBase
{
    private ObservableCollection<DataModel> _data;

    public ObservableCollection<DataModel> Data
    {
        get { return _data; }
        set
        {
            if (_data!= value)
            {
                _data= value;
                OnPropertyChanged("Data");
            }
        }
    }

    public DataViewModel(PagingViewModel pagevm)
    {
        _data = new ObservableCollection<DataModel>();
        _data= pagevm.Data; 
    }

}

My Data属性绑定到DataView.xaml中DataGrid的ItemSource,DataContext设置为DataViewModel。

2 个答案:

答案 0 :(得分:0)

这是一个很好的细节问题!

对于您的刷新问题,我看到一些选项: 在 RefreshData 中设置数据时,您应该使用属性的公共设置器而不是 _data 。如果不这样做,您将永远不会使用OnPropertyChanged来通知视图您的整个集合已更改。

所以你需要替换:

_data= GetData(start, itemCount, out totalItems);

使用:

Data= GetData(start, itemCount, out totalItems);

顺便说一句,你的DataViewModel对我来说毫无意义。您的 _audits 字段无处可见, ObservableCollection Data 永远不会在此ViewModel中设置。我相信你的问题必须来自于此。

PS:

另一方面,我有一些与您的问题没有直接关系的建议:

首先,如果你想在设置它之前检查一些RelayCommand或者是否为null,你可能想要使用??运营商:https://msdn.microsoft.com/en-us/en-en/library/ms173224.aspx

其次我高度建议您将RelayCommands行为放在方法中。当你最终得到十几个命令时,维护RelayCommand是一件非常麻烦的事情,其中​​一切都发生在lambda中。

这样你就会替换它:

public ICommand NextCommand
{
    get
    {
        if (_nextCommand == null)
        {
            _nextCommand = new RelayCommand
            (
                param =>
                {
                    start += itemCount;
                    RefreshData();
                },
                param =>
                {
                    return start + itemCount < totalItems ? true : false;
                }
            );
        }
           return _nextCommand;
    }
}

有了这个:

 public ICommand NextCommand
 {
     get
     {
         return _nextCommand = _nextCommand ?? new RelayCommand(Next, CanExecuteNext);
     }
 }

 private void Next()
 {
     start += itemCount;
     RefreshData();
 }

 private bool CanExecuteNext()
 {
     return start + itemCount < totalItems ? true : false;
 }

答案 1 :(得分:0)

听起来您的命令需要能够访问您的页面视图模型。对于他们这样做,你的分页viewModel需要保存对页面viewModel的引用(这样你的分页viewModel可以将它传递给相关的命令)。

我建议使用属性注入。在你的PagingViewModel中添加一个这样的属性;

    public AuditTrailViewModel AuditTrailViewModel { get; set; }

(如果需要,请提供房产变更通知)。

在您的命令中,您现在可以访问AuditTrailViewModel的属性

public ICommand LastCommand
{
    get
    {
        if (_lastCommand == null)
        {
            _lastCommand = new RelayCommand
            (
                param =>
                {
                    start = (totalItems / itemCount - 1) * itemCount;
                    start += totalItems % itemCount == 0 ? 0 : itemCount;
                    AuditTrailViewModel.Data = //Now you can update your viewModel
                    RefreshData();
                },
                param =>
                {
                    return start + itemCount < totalItems ? true : false;
                }
            );
        }

        return _lastCommand;
    }
}