我有分頁查看用戶控件,它有自己的視圖模型。我在頁面中添加了一個分頁,其中的數據網格也有一個單獨的視圖模型。WPF MVVM分頁
我的問題是我怎麼可以更新的ObservableCollection我有我的頁面視圖模型的每一個命令,在我的分頁視圖模型做的時間?
這裏是我的PagingControl.xaml
<StackPanel Width="Auto"
Orientation="Horizontal">
<Button
Margin="4,0"
Content="<<"
Command="{Binding FirstCommand}"/>
<Button
Margin="4,0"
Content="<"
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=">"
Command="{Binding NextCommand}"/>
<Button
Margin="4,0"
Content=">>"
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");
}
}
下面是我的頁面視圖模型: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;
}
}
我的數據屬性綁定到數據網格中的一個DataView.xaml用的ItemSource的DataContext設置爲DataViewModel。
喜炎yankelevich,是我不好的跟蹤線索的一部分。它應該是DataViewModel的構造函數,我已經編輯它並將嘗試你的答案。謝謝! :) –
我實現了你的答案,但我的問題現在我似乎無法更新DataGrid中的數據是有界的。這些值已經通過,但datagrid沒有根據Observable Collection的新值更新:( –