How to automatically scroll datagrid from view model with template datastudio for WinUI pattern

huangapple go评论81阅读模式
英文:

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

你可以使用 DataGridScrollIntoView 方法。

现在,由于你的视图模型知道何时处理项目,你可以在视图模型中创建一个 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&lt;object&gt;? 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&#39;t forget to name your DataGrid.
        this.DataGridControl.ScrollIntoView(item, column: null);
    }
}

huangapple
  • 本文由 发表于 2023年6月25日 19:31:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/76550176.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定