英文:
How to automatically scroll datagrid from view model with template datastudio for WinUI pattern
问题
我可以帮你了解如何在处理视图模型中的列表时自动滚动数据网格列表。
这里有一个想法,我有一个加载到数据网格的项目列表,然后我按下按钮,从视图模型对每个项目执行一些操作,现在我希望一旦项目在视图模型中被处理,数据网格将滚动到已处理的项目,然后继续直到处理完最后一个项目。
我正在使用带有依赖注入的WinUI模板工作室,问题是我无法想出从视图模型更新视图的正确方法...
在MVVM模式的规则和实践中有点迷失。
英文:
Could you please help me to get an idea of how to autoscroll datagrid list while processing the list from the view model.
Here is an idea, I have a list of items loaded to datagrid, then I press the button and do some operations on each item from the view model, now I want that once item has been processed in view model, the DataGrid has scrolled the list to the processed item and so on until the last item is processed.
I'm using a template studio for WinUI with dependency injection, the problem is that I can;t get an idea of what would be the proper way to update view from view model....
Kind of lost in the MVVM pattern rules and practices.
答案1
得分: 1
你可以使用 DataGrid
的 ScrollIntoView
方法。
现在,由于你的视图模型知道何时处理项目,你可以在视图模型中创建一个 ItemProcessed
事件并订阅该事件。
例如,
ViewModel.cs
public partial class ViewModel : ObservableObject
{
...
public event EventHandler<object>? ItemProcessed;
private void OnItemProcessed(object item)
{
ItemProcessed?.Invoke(this, item);
}
...
}
MainPage.xaml.cs
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
this.ViewModel = new MainPageViewModel();
this.ViewModel.ItemProcessed += ViewModel_ItemProcessed;
}
public MainPageViewModel ViewModel { get; }
private void ViewModel_ItemProcessed(object? sender, Item item)
{
// 不要忘记给你的 DataGrid 命名。
this.DataGridControl.ScrollIntoView(item, column: null);
}
}
英文:
You can use DataGrid
's ScrollIntoView
method.
Now, since your view model knows when an item is "processed", you can create a ItemProcessed
event in your view model and subscribe to this event.
For example,
ViewModel.cs
public partial class ViewModel : ObservableObject
{
...
public event EventHandler<object>? ItemProcessed;
private void OnItemProcessed(object item)
{
ItemProcessed?.Invoke(this, item);
}
...
}
MainPage.xaml.cs
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
this.ViewModel = new MainPageViewModel();
this.ViewModel.ItemProcessed += ViewModel_ItemProcessed;
}
public MainPageViewModel ViewModel { get; }
private void ViewModel_ItemProcessed(object? sender, Item item)
{
// Don't forget to name your DataGrid.
this.DataGridControl.ScrollIntoView(item, column: null);
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论