I have a user control with a search bar and a search button that I am displaying in a window. On the button click I want the event to bubble up to the window where I can do stuff in the window's code behind. I don't want to put the button click code in the user control code behind. How do I do this?
The xaml for the Window looks like this:
<dx:DXWindow
x:Class="Client.Support.AskAQuestionDialog"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:dx="http://schemas.devexpress.com/winfx/2008/xaml/core"
xmlns:libRes="clr-namespace:Shared.Lib.Resources;assembly=Shared.Lib"
xmlns:support="clr-namespace:Client.Support"
xmlns:gui="clr-namespace:Client.GUI;assembly=Client.GUI"
Title="{x:Static libRes:Strings.AskAQuestion}" Loaded="DXWindow_Loaded"
Height="150" Width="700">
<Grid>
<Viewbox>
<support:ZenForumSearchBar VerticalAlignment="Center" HorizontalAlignment="Center"/>
</Viewbox>
</Grid>
</dx:DXWindow>
In the code behind for the UserControl I have this code for the Click event
private void Button_Click(object sender, RoutedEventArgs e)
{
RaiseEvent(e);
}
And in the window that contains the user control I tried to make a handler to handle the event like this
private void ButtonHandler(object sender, RoutedEventArgs e)
{
FrameworkElement fe = e.Source as FrameworkElement;
switch (fe.Name)
{
case "_knowledgeBaseSearchButton":
break;
}
e.Handled = true;
}
I think I am doing everything completely wrong though. How do I get the event that happens in the UserControl to bubble up to the Window where it got instantiated?
Button.Click
is already bubbling event so you don't need Button_Click
in your UserControl
. All you need to do is attach handler in you Window
, Grid
or ViewBox
<Grid Button.Click="ButtonHandler">
and in the handler check e.OriginalSource
instead of e.Source
var fe = e.OriginalSource as FrameworkElement;
and it should work