将数据从 UserControl 传递到另一个 [英] Passing data from a UserControl to another
问题描述
我有一个表单和两个我自己制作的自定义 UserControl
.一个控件有一些按钮,每个按钮的 Tag
属性设置为 PointF
数组.我有另一个 UserControl,它有一个 ObservableCollection<PointF[]>
,我将它的事件处理程序设置为在向其中添加数据时绘制线条.如果我将数据点放在它自己的类上,这很好用...只需确保它有效.
I have a form and two custom UserControl
that I made myself. one control has some buttons that each of these button have theire Tag
property set to an array of 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. 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)
推荐答案
向第一个控件添加事件.
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
}
这篇关于将数据从 UserControl 传递到另一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!