3

Button.MouseDownの投稿を読みました 。続けて、もう1つ質問したいと思います。私のXAMLは次のとおりです。

    <Window.Resources>
    <Style TargetType="{x:Type ContentControl}">
        <Setter Property="BorderThickness" Value="5" />
        <Setter Property="Padding" Value="10" />
        <Setter Property="Background" Value="PaleGreen" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ContentControl}">
                    <Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Padding="{TemplateBinding Padding}">
                        <ContentPresenter/>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</Window.Resources>

<StackPanel Tag="Panel" PreviewMouseDown="PreviewMouseDown" MouseDown="MouseDown">
    <ContentControl BorderBrush="Red" Tag="C1" PreviewMouseDown="PreviewMouseDown" MouseDown="MouseDown">
        <ContentControl BorderBrush="Green" Tag="C2" PreviewMouseDown="PreviewMouseDown" MouseDown="MouseDown">
            <StackPanel Tag="Panel2" PreviewMouseDown="PreviewMouseDown" MouseDown="MouseDown">
                <ContentControl Content="Click Me" BorderBrush="Blue" Tag="C3" PreviewMouseDown="PreviewMouseDown" MouseDown="MouseDown"/>
                <ContentControl Content="Click Me2" BorderBrush="Blue" Tag="C33" PreviewMouseDown="PreviewMouseDown" MouseDown="MouseDown"/>
            </StackPanel>
        </ContentControl>
    </ContentControl>
</StackPanel>

私のcsは次のとおりです:

        private void PreviewMouseDown(object sender, MouseButtonEventArgs e)
    {
        Debug.WriteLine(((FrameworkElement) sender).Tag + " Preview");
    }

    private void MouseDown(object sender, MouseButtonEventArgs e)
    {
        Debug.WriteLine(((FrameworkElement) sender).Tag + " Bubble");
    }

マウスの上下イベントをC3からC33に転送するにはどうすればよいですか?基本的に、1人の兄弟からのすべてのイベントが他の兄弟にも到達するようにします。

4

2 に答える 2

4

C3のEventHandlerから、C33のRaiseEventメソッドを呼び出すことができます。

private void C3_MouseDown(object sender, MouseButtonEventArgs e)
{
  C33.RaiseEvent(e);
}

余談ですが、要素に名前を付けるためにタグを使用していることに気付きました。これに使用するより適切なプロパティは、Nameプロパティです。一般的な規則として、Tagプロパティの使用は、WPFプログラムのおよそ0.001%(100,000分の1)でのみ適切です。それが一般的に使用されるすべての場合において、同じことをするためのはるかに良い方法があります。

于 2010-06-22T07:37:13.677 に答える
1

私はこの種のシナリオの振る舞いを書きました:

using System.Reflection;
using System.Windows;
using System.Windows.Interactivity;

namespace Behaviors
{
    public class RedirectRoutedEventBehavior : Behavior<UIElement>
    {
        public static readonly DependencyProperty RedirectTargetProperty =
            DependencyProperty.Register("RedirectTarget", typeof(UIElement),
                typeof(RedirectRoutedEventBehavior),
                new PropertyMetadata(null));

        public static readonly DependencyProperty RoutedEventProperty =
            DependencyProperty.Register("RoutedEvent", typeof(RoutedEvent), typeof(RedirectRoutedEventBehavior),
                new PropertyMetadata(null, OnRoutedEventChanged));

        public UIElement RedirectTarget
        {
            get => (UIElement) this.GetValue(RedirectTargetProperty);
            set => this.SetValue(RedirectTargetProperty, value);
        }

        public RoutedEvent RoutedEvent
        {
            get => (RoutedEvent) this.GetValue(RoutedEventProperty);
            set => this.SetValue(RoutedEventProperty, value);
        }

        private static MethodInfo MemberwiseCloneMethod { get; }
            = typeof(object)
                .GetMethod("MemberwiseClone", BindingFlags.NonPublic | BindingFlags.Instance);

        private static void OnRoutedEventChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ((RedirectRoutedEventBehavior) d).OnRoutedEventChanged((RoutedEvent) e.OldValue, (RoutedEvent) e.NewValue);
        }

        private void OnRoutedEventChanged(RoutedEvent oldValue, RoutedEvent newValue)
        {
            if (this.AssociatedObject == null)
            {
                return;
            }

            if (oldValue != null)
            {
                this.AssociatedObject.RemoveHandler(oldValue, new RoutedEventHandler(this.EventHandler));
            }

            if (newValue != null)
            {
                this.AssociatedObject.AddHandler(newValue, new RoutedEventHandler(this.EventHandler));
            }
        }

        protected override void OnAttached()
        {
            base.OnAttached();
            if (this.RoutedEvent != null)
            {
                this.AssociatedObject.AddHandler(this.RoutedEvent, new RoutedEventHandler(this.EventHandler));
            }
        }

        protected override void OnDetaching()
        {
            if (this.RoutedEvent != null)
            {
                this.AssociatedObject.RemoveHandler(this.RoutedEvent, new RoutedEventHandler(this.EventHandler));
            }

            base.OnDetaching();
        }

        private static RoutedEventArgs CloneEvent(RoutedEventArgs e)
        {
            return (RoutedEventArgs) MemberwiseCloneMethod.Invoke(e, null);
        }

        private void EventHandler(object sender, RoutedEventArgs e)
        {
            var newEvent = CloneEvent(e);
            e.Handled = true;

            if (this.RedirectTarget != null)
            {
                newEvent.Source = this.RedirectTarget;
                this.RedirectTarget.RaiseEvent(newEvent);
            }
        }
    }
}

使用法:

<ContentControl x:Name="A" Content="Click Me" BorderBrush="Blue">
    <i:Interaction.Behaviors>
        <behaviors:RedirectRoutedEventBehavior RoutedEvent="UIElement.MouseDown" 
                                           RedirectTarget="{Binding ElementName=B}" />
        <behaviors:RedirectRoutedEventBehavior RoutedEvent="UIElement.PreviewMouseDown" 
                                           RedirectTarget="{Binding ElementName=B}" />
    </i:Interaction.Behaviors>
</ContentControl>
<ContentControl x:Name="B" Content="Click Me2" BorderBrush="Blue" />

MouseDownこれにより、PreviewMouseDownイベントがからAにリダイレクトされBます。

Install-Package System.Windows.Interactivity.WPFプロジェクトとxmlns宣言が必要です。

xmlns:behaviors="clr-namespace:Behaviors"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
于 2018-12-03T09:54:31.380 に答える