The wonderful use of

Source: Internet
Author: User

Personal Understanding: As and is

Is is equivalent to "=" in the judgment.

if(e.OriginalSource is Button)

As is generally used to convert another object

e.OriginalSource as Button

To obtain relevant attributes or values.

e.OriginalSource as RadioButton).Name.ToUpper()

The emergence of As and is generally in a routing event. These two keywords need to be used to dynamically generate different UI controls with different types or attributes.

For example, generate a button through

for(int i =  0;i<10;i++){    Button btn = New Button();    btn.Name = i.ToString();
  stackPanel1.Children.Add(btn);
}
for(int i =  0;i<10;i++){  RadioButton btn = New RadioButton();
  btn.Name = i.ToString();
  stackPanel1.Children.Add(btn);
}

Route listening

 this.stackPanel.AddHandler(Button.ClickEvent, new RoutedEventHandler(Btn_Click));

Event Processing

private void Btn_Click(object obj, RoutedEventArgs e){    object Obj = e.OriginalSource;
  if(Obj is Button)
  {
    MessageBox.Show((Obj as Button).Name.ToString());
  }
  if(Obj is RadioButton)
  {
    MessageBox.Show((Obj as RadioButton).Name.ToString());
  }
}

Complex conversions require a layer-by-layer access

Tabcontrol switching requirements
(Sender as tabcontrol). selecteditem as tabitem). header. tostring ();
DataGrid data operation requirements
(Sender as DataGrid). selecteditem as datarowview). Row ["c_id"]. tostring ();

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.