开发者

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜