You don't understand WPF the class hierarchy of framework elements. If WPF, a control is contained in an instance of a class, which itself is not a control. Take for example
Panel
. It contains control, but look at its class hierarchy:
http://msdn.microsoft.com/en-us/library/system.windows.controls.panel.aspx[
^].
So, the question is not posed correctly and cannot be answered like this. Note that you did not explain your ultimate goal. Always do it, then, even if your question is based on some misconception, you will have a chance to get a useful constructive advice anyway.
In this case, this is a bit harder, but I can suggest you to learn certain things in related fields.
First of all, you need to understand WPF
content model:
http://msdn.microsoft.com/en-us/library/bb613548.aspx[
^].
More advanced stuff is the hierarchy of UI framework elements. Perhaps it's more complex that you thought: there are two related trees: the
Logical Tree and the
Visual Tree. They are related but conceptually very different. You can access both of them, for different purposes. Please see:
http://msdn.microsoft.com/en-us/library/ms753391.aspx[
^].
See also:
http://wpftutorial.net/LogicalAndVisualTree.html[
^].
This CodeProject article can also be useful:
Understanding the Visual Tree and Logical Tree in WPF[
^].
In most cases, you never care about those hierarchies, but in some advanced strategies, if, for example, you need to find something in generic UI-agnostic ways, you need to use one tree or another.
[EDIT]
Firstly, you are trying to use wrong conception "find which button was clicked". Instead, you need to write individual event handler to each button. This is the primary use of such events.
Secondly, you are trying to make things way more complex they have to be.
If your layout is already in XAML, add one thing: add
Name
to each of your labels in question. It will give you access the them in code. And you probably already have names for buttons. As your names violate naming conventions, lets assume the names are:
Button buttonFirst =
Button buttonSecond =
Label labelFirst =
Label lableSecond =
The lines above are not in source code, but they will appear in the intermediate auto-generated C# code, generated from your XAML and will be compiled with your project in a usual way. (By the way, good to understand: this is how XAML of the window works.)
Add your events handles to the invocation lists of your events:
buttonFirst.Click += (sender, eventArgs) => {
labelFirst.Content = CalculateText(sender);
};
buttonSecond.Click += (sender, eventArgs) => {
labelSecond.Content = CalculateText(sender);
};
It does not look like code reuse, but it shows you the use of anonymous methods: your method
CalculateText
may have different signature then the handler (anonymous) itself; the code of handler can be anything or the same, you can use
sender
and
eventArgs
or not.
Normally, the even handlers do different things, and no reuse needed. The reuse is needed in more rare situations when the handlers do very similar things, as in your example. But in this case, you can use a loop with arrays (or any other collections) of controls.
And of course you can use the Logical Tree, Visual Tree or both, but analyze the need for it.
If your task itself is so much ad-hock as it seems, don't seek universal solution, but seek them when the problem itself is very universal. For example, do it if you need some methods applicable for any thinkable forms.
—SA