开发者

how to extract a property value from a object sender?

开发者 https://www.devze.com 2023-03-03 12:04 出处:网络
Two different slider controls fires this function below, their names are seektomediaposition and seektomediaposition2.

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 rai开发者_开发知识库sed 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

0

精彩评论

暂无评论...
验证码 换一张
取 消