处理 UserControl 的 ViewModel 中的 DependencyProperty 更改

发布于 2024-09-12 07:43:06 字数 796 浏览 6 评论 0 原文

我目前正在尝试让我的第一个 WPF 用户控件正常工作。它由一些 UI 元素和一个 ViewModel 组成,用于保存数据并完成工作。它有一个项目列表作为输入,另一个项目列表作为输出。当绑定到输入的列表发生变化时,我需要在 ViewModel 中完成某个任务。但我找不到在更改此列表时运行方法的方法。

我认为最好的方法是为输入和输出列表设置 2 个 DependencyProperties。如果输出列表发生变化,则应通知绑定对象,因为它已注册为 DependencyProperty。我想使用 DependencyPropertyChanged 委托来指定 ViewModel 中要在输入更改时执行的方法。

public List<AClass> Input
    {
        get { return (List<AClass>)GetValue(InputProperty); }
        set { SetValue(InputProperty, value); }
    }
public static readonly DependencyProperty InputProperty =
        DependencyProperty.Register("Input", typeof(List<AClass>), typeof(MyUserControl), new UIPropertyMetadata(new List<AClass>(),CallBackDelegate));

我尝试了不同的方法在视图模型构造函数中设置委托,但它们都不起作用。如何在我的视图模型中指定一个方法来在输入列表更改时执行?

Im currently trying to get my first WPF Usercontrol to work. It consists of some UI elements and a ViewModel, holding the data and doing the work. It has a List of items as input, and another List of items as output. I need a certain task in my ViewModel to be done, when the list bound to the input changes. But i can't find a way to run a method at change of this List.

I thought the best way is to have 2 DependencyProperties for the input and output list. If the output list changes, the bound objects should be informed as it is registered as DependencyProperty. And i wanted to use the DependencyPropertyChanged delegate to specify the method in my ViewModel to execute on Input change.

public List<AClass> Input
    {
        get { return (List<AClass>)GetValue(InputProperty); }
        set { SetValue(InputProperty, value); }
    }
public static readonly DependencyProperty InputProperty =
        DependencyProperty.Register("Input", typeof(List<AClass>), typeof(MyUserControl), new UIPropertyMetadata(new List<AClass>(),CallBackDelegate));

I tried different approaches to set the delegate in the viewmodel constructor, but they all did not work. How can I specify a method within my viewmodel to execute on change of input List?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浅语花开 2024-09-19 07:43:07

我将使用 ObservableCollections,而不是 DependencyProperty 中的列表。这会为您提供“自动”通知等。这里是我头脑中可能不太完整的伪代码,可以帮助您入门:

ViewModel

using System.Collections.ObjectModel;

namespace MyNamespace;

class MyViewModel
{
    ObservableCollection<AClass> mInput = new ObservableCollection<AClass>();
    ObservableCollection<AClass> mOutput = new ObservableCollection<AClass>();

    public MyViewModel()
    {
        mInput.CollectionChanged += 
            new NotifyCollectionChangedEventHandler(mInput_CollectionChanged);
    }

    void mInput_CollectionChanged(object sender, 
                                  NotifyCollectionChangedEventArgs e)
    {
        DoSomething();    
    }

    public ObservableCollection<AClass> Input
    {
        get { return mInput; }
    }

    public ObservableCollection<AClass> Output
    {
        get { return mOutput; }
    }
}

控制/视图中的某处

<ItemsControl ItemsSource="{Binding Input}">
    <!-- Rest of control design goes here -->
</ItemsControl>

<ItemsControl ItemsSource="{Binding Output}">
    <!-- Rest of control design goes here -->
</ItemsControl>

控制/查看隐藏代码

class MyViewOrControl
{
    // ViewModel member
    private readonly MyViewModel mViewModel = new MyViewModel();

    // Constructor
    public MyViewOrControl()
    {
        InitializeComponent();
        this.DataContext = mViewModel;
    }

    // Property (if needed)
    public ViewModel
    {
        get { return mViewModel; }
    }
}

Instead of lists in a DependencyProperty, I would use ObservableCollections. That gives you "automatic" notifications, etc. Here is outta-my-head-probably-not-quite-complete pseudo code to get you started:

ViewModel

using System.Collections.ObjectModel;

namespace MyNamespace;

class MyViewModel
{
    ObservableCollection<AClass> mInput = new ObservableCollection<AClass>();
    ObservableCollection<AClass> mOutput = new ObservableCollection<AClass>();

    public MyViewModel()
    {
        mInput.CollectionChanged += 
            new NotifyCollectionChangedEventHandler(mInput_CollectionChanged);
    }

    void mInput_CollectionChanged(object sender, 
                                  NotifyCollectionChangedEventArgs e)
    {
        DoSomething();    
    }

    public ObservableCollection<AClass> Input
    {
        get { return mInput; }
    }

    public ObservableCollection<AClass> Output
    {
        get { return mOutput; }
    }
}

Somewhere in Control/View

<ItemsControl ItemsSource="{Binding Input}">
    <!-- Rest of control design goes here -->
</ItemsControl>

<ItemsControl ItemsSource="{Binding Output}">
    <!-- Rest of control design goes here -->
</ItemsControl>

Control/View Code-Behind

class MyViewOrControl
{
    // ViewModel member
    private readonly MyViewModel mViewModel = new MyViewModel();

    // Constructor
    public MyViewOrControl()
    {
        InitializeComponent();
        this.DataContext = mViewModel;
    }

    // Property (if needed)
    public ViewModel
    {
        get { return mViewModel; }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文