3

I have read the post Button.MouseDown In continuation to that I want to ask 1 more question. My XAML is as follows :

    <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>

My cs is as follows :

        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");
    }

How can I transfer mouse up/down events from C3 to C33? Basically I want all events from 1 sibling to reach other too.

Community
  • 1
  • 1
Nitin Chaudhari
  • 1,497
  • 16
  • 39

2 Answers2

4

From C3's EventHandler you can call C33's RaiseEvent method:

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

As an aside, I notice that you are using Tag to name your elements. A much more appropriate property to use for this would be the Name property. A general rule is that the use of the Tag property is only appropriate in roughtly 0.001% of WPF programs (1 in 100,000). In all cases where it is commonly used there is a much better way to do the same thing.

Ray Burns
  • 62,163
  • 12
  • 140
  • 141
  • Do we have to write those codes for all other event handlers as well? Is there a shortcut to do it just in one function? – Hzzkygcs Aug 18 '21 at 07:36
1

I wrote a behavior for this kind of scenario:

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);
            }
        }
    }
}

Usage:

<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" />

This redirects MouseDown and PreviewMouseDown events from A to B.

You need Install-Package System.Windows.Interactivity.WPF to your project, and xmlns declarations:

xmlns:behaviors="clr-namespace:Behaviors"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
hillin
  • 1,603
  • 15
  • 21