การสร้าง _MouseLeftButtonDown สำหรับสี่เหลี่ยมที่สร้างขึ้นแบบไดนามิกใน WPF

แอปพลิเคชันของฉันแสดง เค้าโครงของพื้นสำนักงาน โดยที่ที่นั่งแสดงเป็นรูปสี่เหลี่ยมผืนผ้า และระยะขอบ ลายเส้น ความสูง ความกว้าง และการจัดตำแหน่งจะถูกบันทึกไว้ในฐานข้อมูลสำหรับรหัสที่นั่งแต่ละรายการ ขึ้นอยู่กับสถานะการปันส่วน การเติมของสี่เหลี่ยมจะเป็นสีแดงหรือเขียว สถานะการจัดสรรและรายละเอียดการเข้าใช้จะถูกบันทึกไว้ใน SQL db ฉันต้องมีวิธี MouseLeftButtonDown แยกกันสำหรับแต่ละสี่เหลี่ยม ซึ่งจะแสดงรายละเอียดผู้เข้าพักให้ฉันดู

//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

person Ram Murti    schedule 12.03.2014    source แหล่งที่มา
comment
ขอแนะนำอย่างยิ่งให้คุณใช้ DataBinding และ ItemsControl ที่เหมาะสม และ DataTemplates ที่เหมาะสม แทนที่จะสร้างและจัดการองค์ประกอบ UI ในโค้ดขั้นตอนใน WPF แนวทางดังกล่าวไม่ได้รับการสนับสนุนเนื่องจากเป็นปัญหาเนื่องจากความซับซ้อนของ Visual Tree และยุ่งยากเนื่องจากมีลักษณะเป็นขั้นตอนซึ่งโดยทั่วไปต้องใช้โค้ดมากกว่าและมีความสวยงามน้อยกว่าแนวทางที่ประกาศโดยใช้ DataBinding โพสต์โค้ดปัจจุบันและ XAML ของคุณ แล้วฉันจะบอกวิธีที่เหมาะสมในการทำสิ่งที่คุณต้องการใน WPF ได้   -  person Federico Berasategui    schedule 12.03.2014
comment
สวัสดี @HighCore ฉันได้รวมโค้ดด้านหลังและ XAML ที่ใช้ในแอปพลิเคชันของฉันแล้ว กรุณาแนะนำวิธีที่ดีกว่า   -  person Ram Murti    schedule 13.03.2014
comment
@HighCore เราสามารถทำได้ดีกว่านี้ไหม?   -  person Ram Murti    schedule 20.03.2014
comment
ฉันจะเตรียมตัวอย่างเล็กๆ น้อยๆ ให้กับคุณ   -  person Federico Berasategui    schedule 20.03.2014


คำตอบ (2)


เมื่อคุณสร้างสี่เหลี่ยมให้เพิ่มตัวจัดการเหตุการณ์:

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

จากนั้นคุณจัดการปุ่มซ้ายของเมาส์ที่นี่:

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

หากคุณเรียกโค้ดนี้เมื่อเริ่มต้นแอปพลิเคชันของคุณ ตัวจัดการเหตุการณ์จะถูกเพิ่มโดยอัตโนมัติให้กับออบเจ็กต์ใหม่ทั้งหมดประเภทสี่เหลี่ยมผืนผ้า:

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

private void OnMouseDown(object sender, MouseButtonEventArgs e)
{
}
person Flat Eric    schedule 12.03.2014
comment
สวัสดี @Flat ฉันได้รับข้อผิดพลาดด้านล่าง:- Specified Visual เป็นลูกของ Visual อื่นหรือรากของ CompositionTarget แล้ว สำหรับฉันดูเหมือนว่า classHandler ได้รับการลงทะเบียนสำหรับสี่เหลี่ยมผืนผ้าแรกที่สร้างขึ้นและในวงที่สองจะมีข้อยกเว้นนี้ ความต้องการของฉันที่นี่คือได้รับตัวจัดการมากเท่ากับจำนวนสี่เหลี่ยม (ขึ้นอยู่กับข้อมูลในฐานข้อมูล) กรุณาแนะนำ - person Ram Murti; 13.03.2014
comment
ฟังก์ชัน 'RegisterClassHandler' จะเพิ่มตัวจัดการนี้ลงในสี่เหลี่ยมผืนผ้าทั้งหมด ไม่ใช่แค่ตัวแรกเท่านั้น ข้อผิดพลาดที่คุณได้รับตามปกติเกิดขึ้นเมื่อคุณพยายามเพิ่มตัวควบคุมหนึ่งรายการให้กับองค์ประกอบหลักหลายรายการ ตัวอย่างเช่น หากคุณเรียก 'seatCanvas.Children.Add(rect);' หลายครั้งสำหรับวัตถุเดียวกัน - person Flat Eric; 13.03.2014
comment
ฉันได้รับปัญหาแล้ว ตอนนี้มันทำงานได้อย่างสมบูรณ์แบบ @แบน - person Ram Murti; 20.03.2014