简体   繁体   English

将参数传递给 UserControl 视图模型

[英]Pass parameter to UserControl viewmodel

I'm writing a monitoring program for a test rig.我正在为测试平台编写监控程序。 I'm using Prism and try to maintain good MVVM practice.我正在使用 Prism 并尝试保持良好的 MVVM 实践。 However, I find it really hard to find a solution for my problem:但是,我发现很难为我的问题找到解决方案:

I have a service that receives measurement values from different sensors periodically.我有一项服务可以定期从不同的传感器接收测量值。 It passes the values with unique ids for each sensor to the data class.它将每个传感器的具有唯一 ID 的值传递给数据 class。

The data class is the backbone of my program.数据 class 是我程序的骨干。 It maintains a list with the last measurement of each sensor, updates their values and notifies value changes.它维护一个列表,其中包含每个传感器的最后一次测量,更新它们的值并通知值更改。 It's a static class so every class has access to it.这是一个 static class 所以每个 class 都可以访问它。

//Rudimentary version of my data class, static and backbone of app.
public static class Data
{
    // Event signaling the change of a measurement, includes modified id in event
    #region event
    public static event EventHandler<MeasurementChangedEventArgs> MeasurementChanged;
    static void OnMeasurementChanged(MeasurementChangedEventArgs e) { MeasurementChanged?.Invoke(typeof(Data), e); }
    #endregion

    // List with custom measurement object containing multiple properties (e.g. Name, Value, Unit)
    private static List<Measurement> measurements = new List<Measurement>();

    // Used by server to set values
    public static void SetMeasurementValue(int id, float value)
    {
        // Get entry(s)
        IEnumerable<Measurement> data = measurements.Where(p => p.ID == id);

        if (0 < data.Count())
        {
            // Set value
            foreach (var item in data) { item.Value = value; }

            // Signal change
            OnMeasurementChanged(new MeasurementChangedEventArgs { id = id });
        }
    }
}

The ui is rather complex. ui比较复杂。 It displays all sensor values, many in different representations.它显示所有传感器值,许多以不同的表示形式。 To make it easier for the developer to expand the program, I created UserControls eg a group of labels displaying name, value and unit of a sensor.为了使开发人员更容易扩展程序,我创建了 UserControls,例如一组显示名称、值和传感器单位的标签。 I want to reuse these so I dont have to create them for every sensor.我想重复使用这些,所以我不必为每个传感器创建它们。 Here is how I#M doing it currently: The UserControl VIEW binds to variables like name, value and unit of the VIEWMODEL.以下是我目前的做法:UserControl VIEW 绑定到 VIEWMODEL 的名称、值和单位等变量。 The VIEWMODEL subscribes to the MeasurementChanged event of the data class. To know what sensor to display the VIEWMODEL need to know what sensor I want to display when I place a UserControl in the visual studio ui editor. VIEWMODEL 订阅了数据 class 的 MeasurementChanged 事件。要知道显示什么传感器,VIEWMODEL 需要知道当我在 visual studio ui 编辑器中放置 UserControl 时我想要显示什么传感器。

How do I tell the VIEWMODEL what sensor it should display, in xaml, when I place the UserControl in the visual studio ui editor?当我将 UserControl 放在 visual studio ui 编辑器中时,如何告诉 VIEWMODEL 它应该显示什么传感器,在 xaml 中?

Thanks!谢谢!

EDIT 1 11.03:编辑 1 11.03:

I already researched a lot but can't find a solution that solves my problem.我已经研究了很多但找不到解决我问题的解决方案。 What I need is something like a dependency property (doesn't work because VIEWMODEL derives from BindableBase) or pass a constructor argument to the VIEMODEL in xaml (not really possible)...我需要的是类似依赖属性的东西(不起作用,因为 VIEWMODEL 派生自 BindableBase)或将构造函数参数传递给 xaml 中的 VIEMODEL(不太可能)...

You could use a PubSubEvent from the IEventAggregator to pass the information from your data class to your ViewModel.您可以使用 IEventAggregator 中的 PubSubEvent 将信息从您的数据 class 传递到您的 ViewModel。

It seem to me that your question is on how to implement drag and drop in MVVM.在我看来,您的问题是关于如何在 MVVM 中实现拖放。 I suggest you to add a reference in your project to GongSolutions.WPF.DragDrop.我建议您在项目中添加对 GongSolutions.WPF.DragDrop 的引用。

then in xaml you can reference that package:然后在 xaml 你可以参考 package:

xmlns:dragDrop="clr-namespace:GongSolutions.Wpf.DragDrop;assembly=GongSolutions.Wpf.DragDrop"

Add this property to the Dragsource (the element you drag from):将此属性添加到 Dragsource(您从中拖动的元素):

dragDrop:DragDrop.IsDragSource="True"

and these to the DropTarget (the element you drop to)这些到 DropTarget (你拖放到的元素)

dragDrop:DragDrop.IsDropTarget="True"
dragDrop:DragDrop.DropHandler="{Binding}"

Your ViewModel should implement the IDropTarget interface:你的 ViewModel 应该实现 IDropTarget 接口:

public class M_ViewModel : BaseViewModel, IDropTarget

And define the following functions:并定义以下函数:

public void DragOver(IDropInfo dropInfo)
{

}

public void Drop(IDropInfo dropInfo)
{

}

Now you should be able to trigger the DropEvent in your viewmodel.现在您应该能够在您的视图模型中触发 DropEvent。 About the information on which sensor the user selected, you can use the information from the selected element when you start the drag (maybe you have a list of elements, you can do a binding to the selected one and use that info).关于用户选择哪个传感器的信息,您可以在开始拖动时使用来自所选元素的信息(也许您有一个元素列表,您可以绑定到所选元素并使用该信息)。 I did it that way, there may be a more elegant solution using DropInfo but I didn't research it.我是这样做的,使用 DropInfo 可能有更优雅的解决方案,但我没有研究它。

Afterwards you give the parameter to the viewmodel of your usercontrol.之后,您将参数提供给用户控件的视图模型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM