1.想让ComboBox在点击同一样Item的时候都能进入一个函数(,而不是通过SelectionIndexChange)

在多次尝试后在生成ComboBoxItem的时候使用ComboBoxItem_PreviewMouseDown

//private void ComboBoxItem_Selected(object sender, RoutedEventArgs e)
//{
// //int i = ((ComboBox)sender).SelectedIndex;
// ComboBoxItem item = sender as ComboBoxItem;
// _ = item.Name;
//}

//private void ComboBoxItem_MouseDown(object sender, MouseButtonEventArgs e)
//{
// ComboBoxItem item = sender as ComboBoxItem;
// _ = item.Name;
//}

//private void ComboBoxItem_MouseUp(object sender, MouseButtonEventArgs e)
//{
// ComboBoxItem item = sender as ComboBoxItem;
// _ = item.Name;
//}

private void ComboBoxItem_PreviewMouseDown(object sender, MouseButtonEventArgs e)
{
//调用对应userControl新建
//MvLeft mvLeft = new MvLeft();
//MusicGrid.Children.Add(mvLeft);
}

不过不知道是不是调试的原因,只要有ComboBoxItem_PreviewMouseDown就不会进入ComboBoxItem_Selected断点

另外ComboBoxItem_PreviewMouseDown在进断点的时候前端会被卡一下,导致选是选了,但选的项不会显示在box中

 

2.后台给新建的控件安放在grid合适的位置

https://docs.microsoft.com/en-us/dotnet/desktop/wpf/controls/?view=netframeworkdesktop-4.8

Label firstNameLabel;
Label lastNameLabel;
TextBox firstName;
TextBox lastName;
Button submit;
Button clear;

void CreateControls()
{
    firstNameLabel = new Label();
    firstNameLabel.Content = "Enter your first name:";
    grid1.Children.Add(firstNameLabel);

    firstName = new TextBox();
    firstName.Margin = new Thickness(0, 5, 10, 5);
    Grid.SetColumn(firstName, 1);
    grid1.Children.Add(firstName);

    lastNameLabel = new Label();
    lastNameLabel.Content = "Enter your last name:";
    Grid.SetRow(lastNameLabel, 1);
    grid1.Children.Add(lastNameLabel);

    lastName = new TextBox();
    lastName.Margin = new Thickness(0, 5, 10, 5);
    Grid.SetColumn(lastName, 1);
    Grid.SetRow(lastName, 1);
    grid1.Children.Add(lastName);

    submit = new Button();
    submit.Content = "View message";
    Grid.SetRow(submit, 2);
    grid1.Children.Add(submit);

    clear = new Button();
    clear.Content = "Clear Name";
    Grid.SetRow(clear, 2);
    Grid.SetColumn(clear, 1);
    grid1.Children.Add(clear);
}

 

3.WPF中的sender简单介绍

https://www.cnblogs.com/panchangtao/articles/3314871.html

按照编程的专业术语来说,sender 指的是触发事件的对象。通俗一点来讲,也就是指引起这个事件发生的那个控件,在此指的就是button1,当用户单击button1这个按钮时,会触发名为“button1_Click”的事件。当我们自定义按钮的属性时,我们可以通过代码的方式来改变,首先先创建Button 类的一个对象,即

Button btn=(Botton)sender;或者

Button btn= sender as Button ;

 

 

4.WPF Combobox选中事件  

可以从以下属性获得

SelectedItem;
SelectedText

https://www.cnblogs.com/SeNaiTes/p/9391990.html

 

 

5.WPF:通过名字找控件

 

 

 

6.字符串类型如何转换为控件类型?【未解决】

https://bbs.csdn.net/topics/260037194

 

    1. Button btn =(Button)this.Controls["groupBox1"].Controls[aa];
    2.  
      if(btn!=null)
    3.  
      string str=btn.Text;

没找到Control属性

 

posted on 2022-02-11 15:04  rin_riceroll  阅读(33)  评论(0编辑  收藏  举报