简体   繁体   中英

how to extract a property value from a object sender?

Two different slider controls fires this function below, their names are seektomediaposition and seektomediaposition2.

public void seektomediaposition_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
    {

string name = Convert.ToString(e.Source.GetType().GetProperty("Name"));//wont return what i need.
MessageBox.Show(name);
if(name=="seektomediaposition")
  // whatever is the code
if(name=="seektomediaposition2")
  // whatever is the code

    }

e.Source.GetType() would return the type Slider.

e.Source.GetType().GetProperty("Name") would return "Name" instead of "seektomediaposition" or whatever the controls name who raised the event to this function.

How can i get the name displayed on that messagebox so i can take my decision based on that?

GetProperty() returns a PropertyInfo object. With that you can call GetValue(e.Source, null).

public void seektomediaposition_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
    {

string name = Convert.ToString(e.Source.GetType().GetProperty("Name").GetValue(e.Source, null));
MessageBox.Show(name);
if(name=="seektomediaposition")
  // whatever is the code
if(name=="seektomediaposition2")
  // whatever is the code

    }

Your Function "seektomediaposition_ValueChanged(object sender, RoutedPropertyChangedEventArgs e)" has an object sender.

so here you can say
if (sender == seektomediaposition) do this else if (sender == seektomediaposition2) do other thing

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM