我正在Microsoft Visual Studio,C#,WPF。
我的主窗口(mainform.cs(具有borderstyle =无,它应该加载几个UserControls到一个面板中(几乎覆盖了整个窗口(。所有这些都处于同一名称空间。这些UserControls中的某些元素需要在Mainform.cs中调用函数的Mousedown事件(在其各自的设计器文件中((该函数使Mousedown'd element'd element拖动整个窗口(。
这只是一个例子。UserControl中的其他项目需要将Mainform.cs的不同功能调用到EventHandler或MouseeVentHandler中。基本上,我需要一个事件会调用(不确定这是此上下文中的正确术语(。
我已经看到了许多解决方案,但是没有任何解决方案(也许是因为我没有使用正确的术语搜索,或者也许是因为我在代码中没有很好地实现它们(。
主窗口中的一些相关代码:
public partial class MainForm : Form
{
private const int HTCAPTION = 0x2;
private const int WM_NCLBUTTONDOWN = 0xA1;
[System.Runtime.InteropServices.DllImport("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
[System.Runtime.InteropServices.DllImport("user32.dll")]
public static extern bool ReleaseCapture();
public void OnMousedown(object sender, MouseEventArgs e)
{
// Allow the form to be draggable
if (e.Button == MouseButtons.Left)
{
ReleaseCapture();
SendMessage(Handle, WM_NCLBUTTONDOWN, HTCAPTION, 0);
}
}
}
...以及来自UserControl Designer文件的一些当前代码:
partial class CopperOrNonCopperSelection
{
private void InitializeComponent()
{
// SelectCopperOrNonCopperLabel is a label in the usercontrol. There's more code here to customize this label.
this.SelectCopperOrNonCopperLabel.MouseDown += new System.Windows.Forms.MouseEventHandler(((MainForm)Parent).OnMousedown);
}
}
当我想在mainform.c中的其他项目上应用相同的函数时,此代码在mainform Designer文件中起作用:
this.ElementName.MouseDown += new System.Windows.Forms.MouseEventHandler((this.OnMousedown);
显然,在UserControl Designer文件中简单地将"此"更改为" Mainform"不起作用。实际上,我尝试了许多其他选择(老实说,不理解它们(,但我不记得它们。
以某种方式,Mousedown事件应在Mainform中执行任何笨拙的函数中的任何事情。我见过许多用户说这是不好的习惯,所以我对其他建议开放(除了将函数复制到usercontrol之外(。
使用上述当前代码,我在... (((MainForm)Parent).OnMousedown)
代码上获得了Delegate to an instance method cannot have null 'this'.'
错误。
我试图理解您的问题。首先,您说WPF。您的代码显示Winforms。
我的建议是使用单例图案。但这不是最好的做法。
我的Mainform.cs看起来像这样:
public partial class MainForm : Form
{
private const int HTCAPTION = 0x2;
private const int WM_NCLBUTTONDOWN = 0xA1;
[System.Runtime.InteropServices.DllImport("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
[System.Runtime.InteropServices.DllImport("user32.dll")]
public static extern bool ReleaseCapture();
public void OnMousedown(object sender, MouseEventArgs e)
{
// Allow the form to be draggable
if (e.Button == MouseButtons.Left)
{
ReleaseCapture();
SendMessage(Handle, WM_NCLBUTTONDOWN, HTCAPTION, 0);
}
}
public static MainForm Instance;
public MainForm()
{
Instance = this;
InitializeComponent();
}
}
我有一个公共静态属性 Instance
,它将是我实例化的Mainform的参考。
在我的USERCONTROL中,我使用此属性访问实例化Mainform类的属性和事件。
public partial class UserControl3 : UserControl
{
public UserControl3()
{
InitializeComponent();
this.MouseDown += new MouseEventHandler(MainForm.Instance.OnMousedown);
this.label1.MouseDown += new MouseEventHandler(MainForm.Instance.OnMousedown);
}
}
获得父控件递归的另一种方法您可以使用此方法:
public static Control GetParentRecursive<T>(Control myControl)
{
Control rControl = null;
if (myControl.Parent != null)
{
if (myControl.Parent.GetType() == typeof(T))
{
rControl = myControl.Parent;
}
else
{
rControl = GetParentRecursive<T>(myControl.Parent);
}
}
else
{
rControl = null;
}
return rControl;
}
这样称呼此方法:
GetParentRecursive<MainForm>(this);