Click here to Skip to main content
15,843,623 members
Home / Discussions / WPF
   

WPF

 
AnswerRe: c# Spotify API not returning correctly. Pin
jschell5-Jun-23 6:01
jschell5-Jun-23 6:01 
QuestionNeed help please regarding your awesome WPF MultiComboBox project :) Pin
Member 1160463425-May-23 6:21
Member 1160463425-May-23 6:21 
AnswerRe: Need help please regarding your awesome WPF MultiComboBox project :) Pin
jeron125-May-23 6:50
jeron125-May-23 6:50 
AnswerRe: Need help please regarding your awesome WPF MultiComboBox project :) Pin
Richard Deeming25-May-23 22:28
mveRichard Deeming25-May-23 22:28 
QuestionNavigationControl - Part 3 Pin
Kevin Marois14-May-23 9:01
professionalKevin Marois14-May-23 9:01 
AnswerRe: NavigationControl - Part 3 Pin
Richard Deeming14-May-23 22:40
mveRichard Deeming14-May-23 22:40 
GeneralRe: NavigationControl - Part 3 Pin
Kevin Marois15-May-23 18:14
professionalKevin Marois15-May-23 18:14 
QuestionBubbling Event Question Pin
Kevin Marois9-May-23 15:32
professionalKevin Marois9-May-23 15:32 
I have a CustomControl which contains another custom control. I want to bubble a click event from the Inner Control and handle it on the Window.

Even better, can I somehow bind to the inner control's ButtonClicked command in the Window's VM?

I found this two SO article. It's close, but it doesn't address custom events:
https://stackoverflow.com/questions/65894582/subscribe-to-any-bubbled-up-wpf-child-control-routed-event-without-an-instance-o

Custom Controls
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:local="clr-namespace:BubblingEventsDemo"
                    xmlns:i="http://schemas.microsoft.com/xaml/behaviors">

    <Style TargetType="{x:Type local:InnerControl}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:InnerControl}">

                    <Border Background="{TemplateBinding Background}"
                            BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}">

                        <Button Content="Click Me"
                                Height="32"
                                Width="75">

                            <i:Interaction.Triggers>
                                <i:EventTrigger EventName="Click">
                                    <i:InvokeCommandAction Command="{Binding ButtonClickedCommand, 
                                                            RelativeSource={RelativeSource TemplatedParent}}"/>
                                </i:EventTrigger>
                            </i:Interaction.Triggers>

                        </Button>

                    </Border>

                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

    <Style TargetType="{x:Type local:OuterControl}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:OuterControl}">

                    <Border Background="{TemplateBinding Background}"
                            BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}">

                        <local:InnerControl Height="100"
                                            Width="150"
                                            Background="LightBlue"/>

                    </Border>

                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

</ResourceDictionary>
Code Behind
using CommunityToolkit.Mvvm.Input;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace BubblingEventsDemo
{
    public class InnerControl : Control
    {
        private ICommand? _ButtonClickedCommand;
        public ICommand ButtonClickedCommand
        {
            get
            {
                if (_ButtonClickedCommand == null)
                    _ButtonClickedCommand = new RelayCommand(ButtonClickedExecuted, ButtonClickedCanExecute);
                return _ButtonClickedCommand;
            }
        }

        public static readonly RoutedEvent ButtonClickedEvent =
                    EventManager.RegisterRoutedEvent("ButtonClicked",
                    RoutingStrategy.Bubble,
                    typeof(RoutedEventHandler),
                    typeof(InnerControl));

        public event RoutedEventHandler ButtonClicked
        {
            add { AddHandler(ButtonClickedEvent, value); }
            remove { RemoveHandler(ButtonClickedEvent, value); }
        }

        static InnerControl()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(InnerControl), 
                new FrameworkPropertyMetadata(typeof(InnerControl)));
        }

        private bool ButtonClickedCanExecute()
        {
            return true;
        }

        private void ButtonClickedExecuted()
        {
            RaiseEvent(new RoutedEventArgs(ButtonClickedEvent));
        }
    }
}
Window XAML
<Window x:Class="BubblingEventsDemo.UI.MainWindowView"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:i="http://schemas.microsoft.com/xaml/behaviors"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:ctrls="clr-namespace:BubblingEventsDemo;assembly=BubblingEventsDemo"
        mc:Ignorable="d"
        Title="MainWindow" 
        Height="450" 
        Width="800">

    <Grid>

        <ctrls:OuterControl Background="Tan"
                            Height="225"
                            Width="300"
                            ctrls:InnerControl.ButtonClicked="OuterControl_ButtonClicked"> <==== THIS CATCHES THE EVENT IN THE WINDOW'S CODE BEHIND

            <i:Interaction.Triggers>

                WHAT I WANT IS TO HANDLE IT IN THE VIEW MODEL. WHAT GOES HERE IN 'EventName'?

                <i:EventTrigger EventName="OuterControl_ButtonClicked">
                    <i:InvokeCommandAction Command="{Binding ButtonClickedCommand, RelativeSource={RelativeSource TemplatedParent}}"/>
                </i:EventTrigger>
            </i:Interaction.Triggers>

        </ctrls:OuterControl>

    </Grid>

</Window>
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.


modified 12-May-23 11:15am.

AnswerRe: Bubbling Event Question Pin
Gerry Schmitz12-May-23 6:28
mveGerry Schmitz12-May-23 6:28 
GeneralRe: Bubbling Event Question Pin
Kevin Marois12-May-23 6:35
professionalKevin Marois12-May-23 6:35 
GeneralRe: Bubbling Event Question Pin
Gerry Schmitz12-May-23 6:46
mveGerry Schmitz12-May-23 6:46 
GeneralRe: Bubbling Event Question Pin
Kevin Marois12-May-23 7:16
professionalKevin Marois12-May-23 7:16 
AnswerRe: Bubbling Event Question Pin
Kenneth Haugland14-May-23 10:55
mvaKenneth Haugland14-May-23 10:55 
GeneralRe: Bubbling Event Question Pin
Kevin Marois14-May-23 11:40
professionalKevin Marois14-May-23 11:40 
GeneralRe: Bubbling Event Question Pin
Kenneth Haugland14-May-23 12:06
mvaKenneth Haugland14-May-23 12:06 
GeneralRe: Bubbling Event Question Pin
Kevin Marois14-May-23 12:08
professionalKevin Marois14-May-23 12:08 
GeneralRe: Bubbling Event Question Pin
Kenneth Haugland14-May-23 12:19
mvaKenneth Haugland14-May-23 12:19 
GeneralRe: Bubbling Event Question Pin
Kevin Marois15-May-23 18:47
professionalKevin Marois15-May-23 18:47 
GeneralRe: Bubbling Event Question Pin
Kenneth Haugland15-May-23 19:46
mvaKenneth Haugland15-May-23 19:46 
GeneralRe: Bubbling Event Question Pin
Kevin Marois16-May-23 8:02
professionalKevin Marois16-May-23 8:02 
QuestionExpander Header Content Stretch Pin
Kevin Marois9-May-23 12:25
professionalKevin Marois9-May-23 12:25 
AnswerRe: Expander Header Content Stretch Pin
Richard Deeming9-May-23 22:48
mveRichard Deeming9-May-23 22:48 
GeneralRe: Expander Header Content Stretch Pin
Kevin Marois13-May-23 7:05
professionalKevin Marois13-May-23 7:05 
QuestionVB.NET WPF drag and drop for LISTBOX and TREEVIEWs..Anyone help with examples? Pin
Member 84579549-May-23 9:51
Member 84579549-May-23 9:51 
AnswerRe: VB.NET WPF drag and drop for LISTBOX and TREEVIEWs..Anyone help with examples? Pin
Richard MacCutchan9-May-23 11:14
mveRichard MacCutchan9-May-23 11:14 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.