你想它怎么冒泡?Grid上面就没有相同事件给它冒泡,冒泡必须是有相同事件才能做到,同时必须将事件定义为路由事件,且策略为RoutingStrategy.Bubble。我这里既看不到你事件的定义,不知道是否正确,又没看到外部有相同事件的控件来接收这样的冒泡事件,根本冒不起来嘛。

解决方案 »

  1.   

    1:首先,我的路由事件是定义为RoutingStrategy.Bubble的(由于篇幅有限,这里就没有列代码)。
    2:其次,Grid没有相同事件给它冒泡,可不可以使用附加事件呢?Grid没有Click事件,但同样可以使用Button.Click响应Click事件。
    3:如果凡事都要外部有相同的事件才能冒泡,那么,就一定是预定义的事件了吧,自定义的路由事件外部怎么会有呢?
      

  2.   

    你先试试你的事件是否能正常工作,也就是将事件处理函数设置到MyControl上面,我不知道你的MyControl 是如何定义出来的,自然无法模仿,我也没那么多时间来写控件测试。如果仅仅要引发内部控件的事件,这应该是可以的,但是这个事件需要内部控件值真的发生了改变,内部事件本身能触发才行,你现在只在外部注册了附件事件,内部没有注册,无法知道内部事件是否触发了,问题说不定是内部事件本身就没有引发而不是无法冒泡。
      

  3.   

    将事件处理函数设置到MyControl上,是能够正常工作的
      

  4.   

    自定义控件:<ResourceDictionary
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WPF3">
        <Style TargetType="{x:Type local:MyControl}">
            <Setter Property="Height" Value="20"/>
            <Setter Property="Width" Value="30"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type local:MyControl}">
                        <Border Background="Red" BorderThickness="1" BorderBrush="Black"/>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </ResourceDictionary>
    public class MyControl : Control
        {
            static MyControl()
            {
                DefaultStyleKeyProperty.OverrideMetadata(typeof(MyControl), new FrameworkPropertyMetadata(typeof(MyControl)));
            }
          //依赖项属性
            public static readonly DependencyProperty ValueProperty = DependencyProperty.Register("Value",
            typeof(double), typeof(MyControl), new FrameworkPropertyMetadata(0, new PropertyChangedCallback(OnValueChanged))); 
            public double Value
            {
                get { return (double)GetValue(ValueProperty); }
                set { SetValue(ValueProperty, value); }
            }
            private static void OnValueChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args) 
            {
                MyControl control = (MyControl)obj;
                RoutedPropertyChangedEventArgs<double> e = new RoutedPropertyChangedEventArgs<double>(
                    (double)args.OldValue, (double)args.NewValue, ValueChangedEvent); 
                control.OnnnnValueChanged(e);
            }
          //路由事件
            public static readonly RoutedEvent ValueChangedEvent = EventManager.RegisterRoutedEvent("ValueChanged", RoutingStrategy.Bubble,
                typeof(RoutedPropertyChangedEventHandler<double>), typeof(MyControl));// 
            public event RoutedPropertyChangedEventHandler<double> ValueChanged
            {
                add { AddHandler(ValueChangedEvent, value); }
                remove { RemoveHandler(ValueChangedEvent, value); }
            }
            protected virtual void OnnnnValueChanged(RoutedPropertyChangedEventArgs<double> args) 
            {
                RaiseEvent(args); 
            }
        }
    主窗体:<Window x:Class="WPF3.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:local="clr-namespace:WPF3"
            Title="MainWindow" Height="350" Width="525">
        <Grid local:MyControl.ValueChanged="grid_ValueChanged">
            <local:MyControl />
            <local:MyControl />
            <local:MyControl />
        </Grid>
    </Window>
    全部的代码就是上面的那些。自定义MyControl中有一个依赖项属性Value,一个路由事件ValueChanged。在主窗体中,Grid里面放了三个MyControl元素,我就想在Grid中集中处理ValueChanged事件。由于Grid并没有ValueChanged事件,所以,在Grid中采用MyControl.ValueChanged附加事件的方式为什么又不行呢?
    如果都还不行,那此处的路由事件到底路由没有呢?
      

  5.   

    那你看下我9楼的代码,在Grid中如何处理其子元素的自定义的路由事件呢?
      

  6.   

    我也遇到这个问题,搜索的时候看到你的帖子,也不知道你解决没有。后来我看到一个替代方案,调用一下AddHandle,用法大概如下:AddHandle(MyControl.ValueChanged, new RoutedEventHandler(grid_ValueChanged)),相当于做一下订阅。