gpt4 book ai didi

c# - WPF MVVM 分页

转载 作者:行者123 更新时间:2023-11-30 18:18:28 25 4
gpt4 key购买 nike

我有一个分页 View 的用户控件,它有自己的 View 模型。我在具有数据网格的页面中添加了分页,该数据网格也具有单独的 View 模型。

我的问题是,每次在我的分页 View 模型中执行命令时,如何更新我在页面 View 模型中的 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");
}
}

这是我的页面的 View 模型: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;
}

}

我的数据属性绑定(bind)到 DataView.xaml 中 DataGrid 的 ItemSource,DataContext 设置为 DataViewModel。

最佳答案

这是一个非常详细的问题!

对于您的刷新问题,我看到了几个选项:当在 RefreshData 中设置您的 Data 时,您应该使用属性的公共(public) setter 而不是 _data。如果不这样做,您将永远不会使用 OnPropertyChanged 来通知 View 您的整个集合已更改。

所以你需要替换:

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

与:

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

顺便说一下,您的 DataViewModel 对我来说毫无意义。您的 _audits 字段无处可见,ObservableCollection Data 从未在此 ViewModel 中设置。我相信你的问题一定来自于此。

附言:

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

首先,当您想在设置之前检查某些 RelayCommand 或 else 是否为 null 时,您可能需要使用 ??接线员:https://msdn.microsoft.com/en-us/en-en/library/ms173224.aspx .

其次,我强烈建议您将 RelayCommands 行为放在方法中。当您最终得到许多命令时,在 lambda 表达式中维护 RelayCommand 真是一团糟。

这样你就可以替换这个:

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;
}

关于c# - WPF MVVM 分页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41357309/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com