0

I create items in grid on code.

Border panel = new Border();
Grid.SetColumn(panel, 3);
Grid.SetRow(panel, 3);

StackPanel stack = new StackPanel();
panel.Child = stack;

Label hasta = new Label();
hasta.Content = str_hasta;
stack.Children.Add(hasta);


Label hastalik = new Label();
hastalik.Content = str_hastalik;
stack.Children.Add(hastalik);

grd_gunluk.Children.Add(panel);

When click this Grid cell, I want to getting label text. How can I do it that?

mm8
  • 163,881
  • 10
  • 57
  • 88
  • My experience is that better to avoid directly manipulate elements, instead of doing this, Bind the View to a Class with the properties you need: WPF is easy when used with a ViewModel + Fody PropertyChanged https://github.com/Fody/PropertyChanged – Tony Feb 14 '18 at 17:22

2 Answers2

0

I think, you expect this,

public MainWindow()
        {
            InitializeComponent();
            Border panel = new Border();
            Grid.SetColumn(panel, 3);
            Grid.SetRow(panel, 3);

            StackPanel stack = new StackPanel();
            panel.Child = stack;

            Label hasta = new Label();
            hasta.Content = "Test";
            hasta.PreviewMouseLeftButtonDown += PreviewMouseLeftButtonDownEvent;
            stack.Children.Add(hasta);


            Label hastalik = new Label();
            hastalik.Content = "MM";
            hastalik.PreviewMouseLeftButtonDown += PreviewMouseLeftButtonDownEvent;
            stack.Children.Add(hastalik);

            Grid.Children.Add(panel);
        }

        protected void PreviewMouseLeftButtonDownEvent(object sender, EventArgs e)
        {
            MessageBox.Show(((Label)sender).Content.ToString());
        }

The Stack panel click event getting the label box value is complex. So use data grid and binding using the Model

UI Page

<DataGrid Name="DataGrid" IsReadOnly="True" >
            <DataGrid.ItemContainerStyle>
                <Style TargetType="DataGridRow">
                    <EventSetter Event="MouseDoubleClick" Handler="Row_DoubleClick"/>
                </Style>
            </DataGrid.ItemContainerStyle>
        </DataGrid>

Code Page

  public MainWindow()
    {
        InitializeComponent();
        List<Student> stud = new List<Student> {
    new Student {RollNo = 1, Name = "Ankur", marks = 34 },
    new Student {RollNo = 2, Name = "Dhrumit", marks = 79},
    new Student {RollNo = 3, Name = "Mannan", marks = 67 }};
        DataGrid.ItemsSource = stud;
    }

    public class Student
    {
        public int RollNo { get; set; }
        public string Name { get; set; }
        public double marks { get; set; }
    }   

    private void Row_DoubleClick(object sender, MouseButtonEventArgs e)
    {
        // execute some code
        var student = (Student)((System.Windows.Controls.DataGridRow)sender).Item;
        MessageBox.Show("RollNo = " + student.RollNo + " Name = " + student.marks + " marks = " + student.marks);
    }

UI Image

UI Image

  • that works but only work when click label. I want to access label when click Stackpanel (stack), getting label text –  Feb 15 '18 at 07:41
0

You could handle the MouseLeftButtonDown event for the Border:

Border panel = new Border();
Grid.SetColumn(panel, 3);
Grid.SetRow(panel, 3);

StackPanel stack = new StackPanel();
panel.Child = stack;

Label hasta = new Label();
hasta.Content = str_hasta;
stack.Children.Add(hasta);


Label hastalik = new Label();
hastalik.Content = str_hastalik;
stack.Children.Add(hastalik);

grd_gunluk.Children.Add(panel);

panel.MouseLeftButtonDown += (ss, ee) =>
{
    string a = hasta.Content.ToString();
    string b = hastalik.Content.ToString();
};
mm8
  • 163,881
  • 10
  • 57
  • 88
  • @kamilkunt: DId you try this or what happened? Please remember to accept the answer and vote it if your issue has been solved. – mm8 Feb 19 '18 at 13:16