public static Readonly RoutedEvent KeypressedEvent = EventManager.RegisterRoutedEvent( "keypressed",RoutingStrategy.Bubble,typeof(KeyEventHandler),typeof(Keyboard));解决方法 步骤1:将事件处理程序添加到“确定”和“取消”按钮public event KeyEventHandler keypressed { add { AddHandler(KeypressedEvent,value); } remove { RemoveHandler(KeypressedEvent,value); } }voID btnAlphaClick(object sender,RoutedEventArgs e) { var btn = (button)sender; Key key = (Key)Enum.Parse(typeof(Key),btn.Content.ToString().toupper()); PresentationSource source = null; foreach (PresentationSource s in PresentationSource.CurrentSources) { source = s; } RaiseEvent(new KeyEventArgs(inputManager.Current.PrimaryKeyboardDevice,source,key));
该控件是一个屏幕键盘,我们基本上需要传递给KeypressedEventArgs给事件的订阅者详细说明按下了什么键(我们找不到太多帮助我们在WPF中使用它,只有winforms).
任何帮助,非常感谢!
public event KeyEventHandler keypressed { add { AddHandler(KeypressedEvent,key));
private voID btnOK_Click(object sender,RoutedEventArgs e){ }private voID btnCancel_Click(object sender,RoutedEventArgs e){ }
在UserControl1.xaml.cs文件中添加公共属性以与主机共享文本框的值
public string Username{ get { return txtname.Text; } set { txtname.Text = value; }}
声明可以通过windows窗体订阅的Ok和Cancel按钮的事件.
public event EventHandler OkClick;public event EventHandler CancelClick;
现在将代码添加到事件处理程序,以便我们也可以将事件提升为主机.
private voID btnOK_Click(object sender,RoutedEventArgs e){ if (OkClick != null) OkClick(this,e);}private voID btnCancel_Click(object sender,RoutedEventArgs e){ if (CancelClick != null) CancelClick(this,e);}
第2步:在windows窗体中处理WPF控件事件
在创建用户控件实例后立即将Handler添加到OKClick和CancelClick Events
_WPFUserControl.OkClick += new EventHandler(OnOkHandler);_WPFUserControl.CancelClick += new EventHandler(OnCancelHandler);
在处理程序方法中编写代码.在这里,我在OK按钮处理程序中使用Username属性,因此也显示了如何共享值.
protected voID OnOkHandler(object sender,EventArgs e){ MessageBox.Show("Hello: " +_WPFUserControl.Username + " you clicked Ok button");}protected voID OnCancelHandler(object sender,EventArgs e){ MessageBox.Show("you clicked Cancel button");}
参考:
http://a2zdotnet.com/View.aspx?Id=79
以上是内存溢出为你收集整理的c# – WPF – 用户控件内部的事件全部内容,希望文章能够帮你解决c# – WPF – 用户控件内部的事件所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)