如何为WPF用户控件创建自定义事件处理程序 [英] How to Create a custom event handler for a WPF usercontrol

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

问题描述

您好我有这个:但不行吗



<前lang =cs> 名称空间 WpfDialControl
{

public partial class DialControlMod_0:UserControl
{
public delegate void ValueChangedEventHandler( object Sender);
public event ValueChangedEventHandler ValueChanged;
....
....

protected 虚拟 void OnValueChanged( object sender)
{
if (ValueChanged!= null
ValueChanged(sender);
}


....
....


public partial class MainWindow:Window
{
public MainWindow()
{
InitializeComponent();
MyKnob.ValueChanged + = new DialControlMod_0.ValueChangedEventHandler(MyKnob_ValueChanged);
}

private void MyKnob_ValueChanged( object Sender)
{
MessageBox.Show( HOLA);

// 测试不起作用
}
}

解决方案

这里没有什么可以起作用。你没有显示任何调用你的方法的代码 OnValueChanged ,这就是问题;你刚才正式定义了这个方法,现在是什么?



你根本不明白事件是如何工作的以及它们的用途。我可以解释一下,但最好只读一下: http://msdn.microsoft.com/en-us /library/awbftdfh.aspx [ ^ ]。



在你学习上​​述材料之前,帮助你的特定问题是没用的; in可能需要学习理解这个主题所需的先决条件; 2)如果您仍然不知道如何定义和使用事件,请描述所需的功能。特别是,如果事件被称为ValueChanged,至少你应该拥有属性 Value ;并且测试代码应该尝试修改此属性的值(更改它,对吗?),并且应该调用该事件。问题不在于属性或事件的名称;我只想给你一个提示。



-SA


多么悲伤来自SA的回答。

如果不浪费时间发布答案,他会好起来的


Hello i have this: but not works

namespace WpfDialControl
{

    public partial class DialControlMod_0 : UserControl
    {
        public delegate void ValueChangedEventHandler(object Sender);
        public event ValueChangedEventHandler ValueChanged;
....
....

        protected virtual void OnValueChanged(object sender)
        {
            if (ValueChanged != null)
                ValueChanged(sender);
        }


....
....


   public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            MyKnob.ValueChanged += new DialControlMod_0.ValueChangedEventHandler(MyKnob_ValueChanged);
        }

        private void MyKnob_ValueChanged(object Sender)
        {
            MessageBox.Show("hola");

//Test not works
        }
    }

解决方案

There is nothing which can "work" here. You don't show any code which calls your method OnValueChanged, that's the problem; you just formally defined this method, now what?

You simply don't understand how events work and what they are used for. I could explain, but better just read about it: http://msdn.microsoft.com/en-us/library/awbftdfh.aspx[^].

Helping with your particular problem is useless before your 1) learn the above material; in might require to learn the prerequisites required for understanding of this topic; 2) if you still don't know how to define and use events by that time, describe required functionality. In particular, if the event is called "ValueChanged", at least you should have the property Value; and the testing code should try to modify the value of this property ("Change" it, right?), and that should invoke the event. The problem is not the names of properties or events; I'm just trying to give you a hint.

—SA


What a sad answer from SA.
He would have be better off not even wasting his time posting an answer


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

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