tags:

views:

66

answers:

1

Hi,

let's assume I've got a UserControl like this:

<UserControl x:Class="SomeNamespace.SomeClass">
    <Grid>
        <TextBlock Name="SampleTextBlock" />
    </Grid>
</Usercontrol>

Somehwere in my application I get handed the Name: SampleTextBlock. I can find the corresponding Framework-Element to the name. But how can I find the Type of the Code-Behind class from that Framework-Element?

This is what I am looking for in the specified sample:

Type usercontrolTypeOfElement = typeof(SomeClass);

But how to get SomeClass from the Frameworkelement of the TextBlock named SampleTextBlock?

+1  A: 

Do you mean something like this:

FrameworkElement  current=textBlockInstance; // your TextBlock
while(null != current && !(current is UserControl)){
 current=current.Parent as FrameworkElement;
}
if(null != current){
    Type typeOfUserControl=current.GetType();
    MessageBox.Show(typeOfUserControl.Name);
}
HCL
Yes, that's it. I was hoping there's a more elegant way than to traverse the parents, but I guess not.Thanks for your help.
Falcon