简体   繁体   English

将数据从UserControl传递到另一个

[英]Passing data from a UserControl to another

I have a form and two custom UserControl that I made myself. 我有一个表单和两个我自己制作的自定义UserControl one control has some buttons that each of these button have theire Tag property set to an array of PointF . 一个控件有一些按钮,每个按钮都有一个Tag属性设置为PointF数组。 I have another UserControl that has a ObservableCollection<PointF[]> that I set its event handler to draw the lines if data is being added to it. 我有另一个UserControl有一个ObservableCollection<PointF[]>我设置了它的事件处理程序,以便在添加数据时绘制线条。 This works fine If I put the data points on its own class...just make to sure it works. 这工作正常如果我把数据点放在自己的类上...只是确保它的工作原理。

No my problem is, having this two control in one form, how can I set the click event of buttons in the first control, to add data points to the second control? 没有我的问题,在一个表单中有这两个控件,如何在第一个控件中设置按钮的单击事件,将数据点添加到第二个控件?

This two controls are both in two different projects in my soloution. 在我的解决方案中,这两个控件都在两个不同的项目中。 and the form that these to controls are being showed in, is also in a different project (it is the launching project of soloution) 并且这些控制的形式正在展示中,也在一个不同的项目中(它是解决方案的启动项目)

You could add a public method on the second usercontrol that receive an array of PointF, then inside this method you could add the PointF to your collection. 您可以在接收PointF数组的第二个usercontrol上添加一个公共方法,然后在此方法中可以将PointF添加到您的集合中。

EDIT: To handle the click event inside the first user control 编辑:处理第一个用户控件中的单击事件

inside the first usercontrol add the event and the delegate required 在第一个usercontrol内添加事件和所需的委托

public delegate void OnClickPointDataEvent(object sender, PointF[] data);
public event OnClickPointDataEvent ClickPointData;

then form_load event subscribe to the usercontrol1 event 然后form_load事件订阅usercontrol1事件

uc1.ClickPointData += new UserControl1.OnClickPointDataEvent(form_subscribe_event);

private void form_subscribe_event(object sender, PointF[] data)
{
    uc2.SomePublicMethod(data);
}

and finally, inside the first usercontrol button click call the code that handle the event inside the form 最后,在第一个usercontrol按钮内单击调用处理表单内事件的代码

....
if(ClickPointData != null)
    ClickPointData(pointf_array);
...

Add an event to the first control. 将事件添加到第一个控件。

public event EventHandler<EventArgs> Control1ButtonClicked;
private void OnClicked()
{
    var handler = Control1ButtonClicked;
    if (handler != null)
    {
        handler(this, new EventArgs());
    }
}

private void Button1_Click(object sender, EventArgs e)
{
    OnClicked();     
}

Add a property to the second control 将属性添加到第二个控件

public ObservableCollection<PointF[]> MyPoints{ get; set;};

Then in your main application add a listener 然后在主应用程序中添加一个监听器

userControl1.Control1ButtonClicked += new EventHandler<EventArgs>(userControl1_Control1ButtonClicked);

void userControl1_Control1ButtonClicked()
{
    //Do Something to control 2
    userControl2.MyPoints.Add() = //Whatever
}

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

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