小编Rub*_*ben的帖子

禁用事件冒泡c#wpf

我遇到以下问题:

当我拿到两个标签时:

<Label x:Name="First" MouseUp="Label_MouseUp">
    <Label x:Name="Second" MouseUp="Label_MouseUp_1">This is a label into another label</Label>
</Label>
Run Code Online (Sandbox Code Playgroud)

以下代码:

private void Label_MouseUp(object sender, MouseButtonEventArgs e)
{
    Console.WriteLine("Do NOT show me");
}

private void Label_MouseUp_1(object sender, MouseButtonEventArgs e)
{
    Console.WriteLine("Show me");
}
Run Code Online (Sandbox Code Playgroud)

当我点击"秒"时,我希望它只触发"Label_MouseUp_1".但在我的控制台中,我得到:

告诉我
不要告诉我

有没有办法关闭冒泡事件?

(另外,"First"必须是可点击的,所以删除那里的事件并不能解决问题)

日Thnx

c# wpf event-bubbling

10
推荐指数
1
解决办法
8328
查看次数

转换类型

我有以下问题.
我有一个班级"Instellingen",这是其他3个班级的一个领域

    public class Instellingen
    {
        private int _ID;
    }
    public class Class1: Button
    {
        private Instellingen _Instellingen;
    }
    public class Class2 : Label
    {
        private Instellingen _Instellingen;
    }
    public class Class3 : TextBox
    {
        private Instellingen _Instellingen;
    }
Run Code Online (Sandbox Code Playgroud)

如果我有另一个类,它使用其他类(但它可以是这3个类中的任何一个)我是否必须使用开关?或者有更简单的方法吗?

    public class AnotherClass
    {
        public AnotherClass ()
        {
            GetInstellingenFromClass(new Class1());
            GetInstellingenFromClass(new Class2());
            GetInstellingenFromClass(new Class3());
        }
        private void GetInstellingenFromClass(Control c)
        {
            switch (c.GetType.ToString())
            {
                case "Class1":
                    Class1 klasse = (Class1) c;
                    //Do something with the _Instellingen of this class
                    break;
                case …
Run Code Online (Sandbox Code Playgroud)

c# type-conversion

1
推荐指数
1
解决办法
153
查看次数

标签 统计

c# ×2

event-bubbling ×1

type-conversion ×1

wpf ×1