[英]WPF allow child user control to control specific keypress events of its parent
[英]How to send all events of a child control to its parent control (custom control)?
我有一個包含三個子控件(帶有PictureBox控件和Label的面板)的自定義控件,並且我想將一個子控件的所有事件發送到其父控件(自定義控件)。
我知道有關此問題的答案很多,但我無法通過簡單的解決方案來解決。
這是我的例子
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
UserControl1 uc1 = new UserControl1();
this.Controls.Add(uc1);
}
}
public partial class UserControl1 : UserControl
{
public PictureBox ChildPictureBox { get; set; }
public UserControl1()
{
PictureBox pictureBox1 = new PictureBox();
pictureBox1.Size = new Size(150, 150);
pictureBox1.BackColor = Color.Red;
pictureBox1.Click += PictureBox1_Click;
this.Controls.Add(pictureBox1);
ChildPictureBox = pictureBox1;
this.Click += UserControl1_Click;
}
private void UserControl1_Click(object sender, EventArgs e)
{
MessageBox.Show("User control click");
}
private void PictureBox1_Click(object sender, EventArgs e)
{
MessageBox.Show("pic clicked");
}
}
以下代碼為示例,此處UserControl1
具有PictureBox
和Panel
,其單擊事件已掛接到MainForm
即MyForm
中。 您可以根據需要對其進行修改。
UserControl1.cs
public partial class UserControl1 : UserControl
{
public delegate void PictureBoxClickHandler(object sender, EventArgs e);
public event PictureBoxClickHandler PictureBoxClick;
public delegate void PanelClickHandler(object sender, EventArgs e);
public event PanelClickHandler PanelClick;
public delegate void PictureBoxDoubleClickHandler(object sender, EventArgs e);
public event PictureBoxDoubleClickHandler PictureBoxDoubleClick;
public delegate void PictureBoxMouseMoveHandler(object sender, MouseEventArgs e);
public event PictureBoxMouseMoveHandler PictureBoxMouseMove;
public UserControl1()
{
InitializeComponent();
}
private void pictureBox1_Click(object sender, EventArgs e)
{
if (PictureBoxClick != null)
{
PictureBoxClick(sender, e);
}
}
private void panel1_Click(object sender, EventArgs e)
{
if (PanelClick != null)
{
PanelClick(sender, e);
}
}
private void pictureBox1_DoubleClick(object sender, EventArgs e)
{
if (PictureBoxDoubleClick != null)
{
PictureBoxDoubleClick(sender, e);
}
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (PictureBoxMouseMove != null)
{
PictureBoxMouseMove(sender, e);
}
}
}
MyForm.cs
public class MyForm : Form
{
public MyForm()
{
InitializeComponent();
var userControl1 = new UserControl1();
Controls.Add(userControl1);
userControl1.PictureBoxClick += userControl1_PictureBoxClick;
userControl1.PanelClick += userControl1_PanelClick;
userControl1.PictureBoxDoubleClick+=userControl1_PictureBoxDoubleClick;
userControl1.PictureBoxMouseMove+=userControl1_PictureBoxMouseMove;
}
private void userControl1_PanelClick(object sender, EventArgs e)
{
//Click: Panel on userControl1
}
private void userControl1_PictureBoxClick(object sender, EventArgs e)
{
//Click: PictureBox on userControl1
}
private void userControl1_PictureBoxMouseMove(object sender, MouseEventArgs e)
{
throw new NotImplementedException();
}
private void userControl1_PictureBoxDoubleClick(object sender, EventArgs e)
{
throw new NotImplementedException();
}
}
編輯:
public partial class UserControl1 : UserControl
{
public PictureBox ChildPictureBox { get; set; }
public UserControl1()
{
InitializeComponent();
ChildPictureBox = pictureBox1;
}
//----
}
現在形式
public class MyForm : Form
{
public MyForm()
{
InitializeComponent();
PictureBox pictureBox = userControl1.ChildPictureBox;
//now work with pictureBox here
pictureBox.Click += pictureBox_Click;
}
private void pictureBox_Click(object sender, EventArgs e)
{
throw new NotImplementedException();
}
}
我認為您可以執行此操作,但是您應該知道,罕見事件不能鏈接到其他控件。
public partial class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent();
foreach (Control control in this.Controls)
{
control.Click += new EventHandler(control_Click);
}
}
private void control_Click(object sender, EventArgs e)
{
this.UserControl1_Click(sender, e);
}
private void UserControl1_Click(object sender, EventArgs e)
{
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.