WPF: how to create events for custom user controls?

I created a UserControl that consists of ItemsCollection . Each item in this collection consists of a ListBox .

My application is presented by Window , which contains a UserControl . I want to be able to manage events related to items inside a ListBox . How can i achieve this? (I'm not sure if this is relevant or not, but UserControl is in an assembly other than the application.)

Here's the UserControl code:

 <UserControl x:Class="UserControls.CalendarMonthViewControl.CalendarMonthView" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" mc:Ignorable="d" d:DesignWidth="580" d:DesignHeight="300" xmlns:calendarMonthViewControl="clr-namespace:UserControls.CalendarMonthViewControl" Name="CalendarMonthViewControl"> <Grid> <ItemsControl ItemsSource="{Binding ElementName=CalendarMonthViewControl, Path=Days}"> <ItemsControl.ItemsPanel> <ItemsPanelTemplate> <UniformGrid Rows="6" Columns="7" /> </ItemsPanelTemplate> </ItemsControl.ItemsPanel> <ItemsControl.ItemTemplate> <DataTemplate DataType="{x:Type calendarMonthViewControl:Day}"> <Border> <Grid> <ListBox ItemsSource="{Binding Path=CalendarDayItems}" /> </Grid> </Border> </DataTemplate> </ItemsControl.ItemTemplate> </ItemsControl> </Grid> </UserControl> 
+4
source share
3 answers

@vlad is on the right track.

Here are a couple of options. Both are related to the handling of routed events.

To handle the routed event, you use the name of the owner class, followed by the name of the event.

The first option is to simply handle the selected event changes (or another ListBox event) in the Window class:

 <Window ... ListBox.SelectionChanged="OnChildListboxSelectionChanged"> ... </Window> 

The second option (a more typical approach) is to process ListBox events inside the UserControl, and then somehow aggregate them and fire the event at this level. This event is then handled by Window . This event can be a routed event or a standard .NET event.

 <UserControl ... ListBox.SelectionChanged="OnChildListBoxSelectionChanged"> ... </UserControl> 

User Management Code:

 public event EventHandler<MyArgTypeEventArgs> ListBoxUpdated; private void OnChildListBoxSelectionChanged(object sender, SelectionChangedEventArgs e) { // aggregate data and info MyArgTypeEventArgs handler = ListBoxUpdated; if (handler != null) handler.Invoke(this, GenerateArgs()); } 

Window handles this event:

 <Window ... ListBoxUpdated="OnListBoxUpdated"> ... </Window> 

This should give you something to start with.

+2
source

I have not used them a lot, but I think RoutedEvents will solve your problem. Events exit your ListBox in the Window (or another element below in the tree), where you can handle them.

edit : quote from link: To add an event handler for an event using XAML, you declare the event name as an attribute of the element that is the event listener. The attribute value is the name of the handler method used, which must exist in the partial class of the code file.

since UserControl inherits from UIElement , I assume something like this will work (untested atm):

 <UserControl x:Class="UserControls.CalendarMonthViewControl.CalendarMonthView" [...] ListBox.NameOfEvent="EventHandlerName"> 
+1
source

I don’t think you are trying to make the right approach, as it creates an unnecessary relationship between your views and means that your UserControl is not encapsulated correctly.

In my opinion, a good solution would be to process the events in your viewControl UserControl model and establish a link between the viewmodel of your Window as necessary so that the views themselves are independent.

0
source

Source: https://habr.com/ru/post/1345718/


All Articles