Question

My application shows the layout of an office floor where the seats are depicted as a rectangle and the margins, strokes, height, width and alignment are saved in the database for each Seat ID. Based on the allocation status, the fill of the rectangle is either red or green. Allocation status and the occupancy details are saved in the SQL db. I need to have separate MouseLeftButtonDown method for each rectangles. which will show me the occupant details.

//Code Behind
public SeatUserControl()
        {
         string cubeId = "";
         string status = "";
         string name = "";
         string number = "";
         int height = 0;
         int width = 0;
         int leftMargin = 0;
         int topMargin = 0;
            InitializeComponent(); 
            SqlDataAdapter data = new SqlDataAdapter();
            DataTable dt = new DataTable();
            string SqlQuery = "select c.SeatId,c.Height,c.Width,c.Stroke,c.MarginTop,c.MarginLeft,e.Status,e.EmpName,e.EmpNumber from SeatDetails c Join MasterData e ON c.SeatId =e.SeatId";
            SqlConnection sqlconn = new SqlConnection(connectionstring);
            sqlconn.Open();
            data = new SqlDataAdapter(SqlQuery, sqlconn);
            data.Fill(dt);
            try
            {
                foreach (DataRow row in dt.Rows)
                {
                    leftMargin = int.Parse(row["MarginLeft"].ToString());
                    topMargin = int.Parse(row["MarginTop"].ToString());
                    height = int.Parse(row["Height"].ToString());
                    width = int.Parse(row["width"].ToString());
                    status = row["Status"].ToString();
                    cubeId = row["SeatId"].ToString();
                    name = row["EmpName"].ToString();
                    number = row["EmpNumber"].ToString();
                    PlaceRectangles();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            sqlconn.Close();
        }
        public string PlaceRectangles()
        {
            Rectangle rect = new Rectangle();
            rect.Height = height;
            rect.Width = width;
            SolidColorBrush StrokeBrush = new SolidColorBrush(Colors.Black);
            rect.Stroke = StrokeBrush;
            rect.VerticalAlignment = VerticalAlignment.Top;
            rect.HorizontalAlignment = HorizontalAlignment.Left;
            rect.Margin = new Thickness(leftMargin, topMargin, 0, 0);
            rect.RadiusX = 8;
            rect.RadiusY = 5;
           if (status.Equals("Allocated"))
            {
                SolidColorBrush myBrush = new SolidColorBrush(Colors.RoyalBlue);
                rect.Fill = myBrush;
            }
           else if (status.Equals("Available"))
            {
                SolidColorBrush myBrush = new SolidColorBrush(Colors.Red);
                rect.Fill = myBrush;
            }
           else 
            {
                SolidColorBrush myBrush = new SolidColorBrush(Colors.White);
                rect.Fill = myBrush;
            }
            seatCanvas.Children.Add(rect);
     }
}

//XAML
<UserControl x:Class="SpaceAllocator.SeatUserControl"
             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:DesignHeight="580" d:DesignWidth="800">
    <Grid Height="580" Width="800">
        <Canvas Name="seatCanvas" Height="580" Width="800" Margin="0,3,-2,78">       </Canvas>
    </Grid>
</UserControl>
enter code here
Was it helpful?

Solution

When you create a rectangle add an event handler:

System.Windows.Shapes.Rectangle rect = new System.Windows.Shapes.Rectangle();
rect.MouseLeftButtonDown += rect_MouseLeftButtonDown;
// apply margins and what not

Then you handle your mouse left button down here:

void rect_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
      var rect = sender as System.Windows.Shapes.Rectangle;
      // do whatever
}

OTHER TIPS

If you call this code at startup of your application, an event handler is added automatically to all new objects of type Rectangle:

EventManager.RegisterClassHandler(typeof(Rectangle), MouseLeftButtonDownEvent, 
    new MouseButtonEventHandler(OnMouseDown), false);

private void OnMouseDown(object sender, MouseButtonEventArgs e)
{
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top