0

我在代码的网格中创建项目。

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);

单击此网格单元格时,我想获取标签文本。我该怎么做?

4

2 回答 2

0

您可以处理以下MouseLeftButtonDown事件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();
};
于 2018-02-15T16:34:26.003 回答
0

我想,你期待这个,

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());
        }

获取标签框值的堆栈面板单击事件很复杂。所以使用数据网格和使用模型绑定

用户界面页面

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

代码页

  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);
    }

用户界面图像

用户界面图像

于 2018-02-14T17:33:08.310 回答