WPF中动态创建的用户控件的事件处理程序 [英] Event handler for Dynamically created User Control in WPF

查看:61
本文介绍了WPF中动态创建的用户控件的事件处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,
我已经动态创建了一个用户控件,并添加了视频作为其内容.
现在,我想为用于拖放的用户控件编写Eventhandler.

//创建用户控件并将视频作为内容加载

Hello,
i have created a usercontrol dynamically and added a video as its content.
Now i want to write Eventhandler for the usercontrol for Drag and dropping it.

// creating user control and loadeing video as content

UserControl uc=new UserControl();
                uc.Height=100;uc.Width =100;           
                MediaElement m1=new MediaElement();
                m1.LoadedBehavior=MediaState.Play;			    
	        m1 .Height =100;m1 .Width =100;			   
	       Uri obj1=new Uri (fd1 .FileName);//loaded from open file dialog box
			    m1 .Source =obj1;
			    uc .Content =m1 ;
			    uniformgrid1 .Children .Add (uc);
			    Mainmedia.Source=obj1 ;
			    uc .MouseMove +=new MouseEventHandler(uc_MouseMove);



拖动代码:



Drag code:

private void uc_MouseMove(object sender, MouseEventArgs e)
            { 
 //here it is showing error type or namespace uc cannot be found.          
               uc dragableusercontrol=sender as uc;  
               	if(dragableusercontrol!=null && e .LeftButton == MouseButtonState.Pressed )
               	{
               		DragDrop .DoDragDrop(dragableusercontrol ,
               		                     new DropableCanvasDragDropData(dragableusercontrol .Parent as Panel ,
               		                                                    dragableusercontrol ,e.GetPosition (dragableusercontrol )),
               		                     DragDropEffects.Copy);
              	}
            }


如何访问事件处理程序中动态创建的usercontrol对象.
如果不是动态创建的用户控件.拖动代码运行正常.
任何建议将不胜感激.

谢谢,


how to access dynamically created usercontrol object in event handler.
if it is not dynamically created user control. the drag code is working perfectly.
Any suggestions will be much appreciated.

thanks,

推荐答案

请尝试以下操作:

Please try the following:

private void uc_MouseMove(object sender, MouseEventArgs e)
            { 
                //Error fixed.          
                UserControl dragableusercontrol = sender as UserControl;  
               	if(dragableusercontrol!=null && e .LeftButton == MouseButtonState.Pressed )
               	{
               		DragDrop .DoDragDrop(dragableusercontrol ,
               		                     new DropableCanvasDragDropData(dragableusercontrol .Parent as Panel ,
               		                                                    dragableusercontrol ,e.GetPosition (dragableusercontrol )),
               		                     DragDropEffects.Copy);
              	}
            }


uc只是UserControl的引用对象,您将其用作Type(事实并非如此).因此,您必须将其强制转换为UserControl类型.


uc is just a reference object of UserControl and you are using it as a Type which is not the case. Hence you have to cast it to UserControl type.


所有事件处理程序都会传递一个名为sender的对象.这是创建事件的对象.将其转换为正确的类型,然后就可以使用.看起来您知道并且您正在这样做.动态创建的控件没有任何不同的理由.它怎么不起作用?发送者的类型不正确吗?您正在创建一个UserControl,并尝试将其强制转换为uc.为什么它们是同一件事?为什么不将UserControl uc =发送者作为UserControl?调试器中对象的类型是什么?

读取错误时,您的编译时问题是您使用的是变量名而不是类型,而使用的是uc而不是UserControl.我怀疑它在您不强制发送者之前已经起作用,但是抓住了您知道其名字的控件.
All event handlers get passed an object called sender. This is the object that created the event. Cast it to the right type, and there you have it. Looks like you know that and you''re doing that. There''s no reason for a dynamically created control to be any different. How does it not work ? Is sender not of the right type ? You are creating a UserControl, and trying to cast it as uc. Why are they the same thing ? Why not UserControl uc = sender as UserControl ? What is the type of the object in the debugger ?

Reading the error, your compile time problem is that you are using the variable name instead of the type, uc instead of UserControl. I suspect it worked before bc you did not cast the sender, but grabbed a control you knew the name of.


这篇关于WPF中动态创建的用户控件的事件处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆