【发布时间】:2011-01-25 18:20:02
【问题描述】:
在 WPF 应用程序中,我在 Grid 中有一堆 CustomControl。为了处理鼠标点击它们,我使用 Grid 的 MouseLeftButtonDown 事件,并在事件处理程序中检查点击了哪个 CustomControl:
private void Grid_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
FrameworkElement feSourceComm = e.Source as FrameworkElement;
MyCustomControl SCurrentComm = new MyCustomControl();
try
{
SCurrentComm = (MyCustomControl)feSourceComm;
}
catch (Exception)
{
...
当我将所有 CustomControls 放在 UserControl 中,然后放在 Grid 中时,问题就出现了。在这种情况下,方法不起作用。
我通过e.Source.GetType().ToString();检查了每种情况下的点击源类型,并得到以下结果:
没有问题的时候(如果我把CustomControls放在没有UserControl的Grid中)
MyProjectNamespace.MyCustomControl
当我将 CustomControls 放入 UserControl 然后放入 Grid
MyProjectNamespace.UserControls.MyUserControlName
当我将 CustomControls 放入 UserControl 然后放入 Grid 并通过 XamlReader.Load从外部文件加载 UserControl
System.Windows.Controls.UserControl
所以,我的问题是:
当 CustomControls 位于 UserControl 内部时,如何使它们显示为 e.Source?
【问题讨论】:
标签: c# wpf user-controls custom-controls frameworkelement