Hello. I want to create a UIElement containing, for example, 2 standard ellipses:

public class MyUI : UIElement { Ellipse el; Ellipse el1; public MyUI(Canvas canv) { Random rnd = new Random(); el = new Ellipse(); el1 = new Ellipse(); el.Width = 30; el1.Width = 30; el.Height = 30; el1.Height = 30; el.Fill = Brushes.Green; el1.Fill = Brushes.Red; canv.Children.Add(el); canv.Children.Add(el1); Canvas.SetTop(el, rnd.Next(300)); Canvas.SetTop(el1, rnd.Next(300)); Canvas.SetLeft(el, rnd.Next(300)); Canvas.SetLeft(el1, rnd.Next(300)); } } 

Tell me how to create a mouse click event on my element (let's call it MyUI_MouseLeftButtonDown), which will be triggered when clicking on one of the ellipses? I suspect that you can somehow use the standard Ellipse_MouseLeftButtonDown events for each of the ellipses.

  • one
    Create UserControl, there in xaml and write down everything you need - Gardes
  • list everything you need, but what do you need to register? - Dmitry Leechman

1 answer 1

Let's define our own events, which will be generated by the MouseLeftButtonDown standard event MouseLeftButtonDown :

 public partial class TwoEllipses : UserControl { //наше новое событие public event EventHandler LeftEllipseClicked; protected void OnLeftEllipseClicked() { if (LeftEllipseClicked != null) { LeftEllipseClicked(this, EventArgs.Empty); } } public TwoEllipses() { InitializeComponent(); } //обработчик стандартного события MouseLeftButtonDown private void Ellipse_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { //генерируем наше новое событие OnLeftEllipseClicked(); } } 

UserControl:

 <UserControl x:Class="WpfApplication.TwoEllipses" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <Grid> <Canvas> <Ellipse Width="115" Height="160" Fill="Blue" MouseLeftButtonDown="Ellipse_MouseLeftButtonDown"> </Ellipse> <Ellipse Width="115" Height="160" Fill="Blue" Canvas.Left="160" MouseLeftButtonDown="Ellipse_MouseLeftButtonDown"> </Ellipse> </Canvas> </Grid> </UserControl> 

We use:

 <Window x:Class="WpfApplication.MainWindow" xmlns:local="clr-namespace:WpfApplication" > <Window.Resources> </Window.Resources> <Grid> <local:TwoEllipses LeftEllipseClicked="TwoEllipses_LeftEllipseClicked"/> </Grid> 

  • It's all? And when we insert this control into our Window, for example, we will need the LeftEllipseClick event. How to do it? - Andrei NOP
  • @ Andrei, I do not know such an event, what do you mean? if you need another event, sign up in the same way - Gardes
  • Well, as I understand it, the author needs to create events in his control, for example, such as LeftEllipseClick (or LeftEllipseClick he calls them there). And then from the Window to have the opportunity to subscribe to them. How to do it? - Andrey NOP
  • @Andrey, I understand) then you need to do something like a DependencyProperty for the event. Unfortunately, I did not encounter a similar task. I'll write the answer when I study - Gardes
  • , everything turned out to be easier) - Gardes