Reputation: 7424
If a control is not given a name or an id is it still unique, or better yet can it be accessed at all in code? Also if it still identifiable what determines its identity in relationship to other controls that don't have names or ids?
Upvotes: 0
Views: 1131
Reputation: 53699
Technically yes they are, each instance of a control is a unique instance of a class. You can access the control through the reference. The trick will be identifying which unique control is the one you are interested in, since it does not have a name or id.
It is the reference to the control which uniquely identifies the control regardless of the ID/Name, and it is the position of the control in the control hierarchy that defines the relationship of between controls. For example the root control might have zero or more child controls and each of those controls might have child controls of there own. The name is a convenient means to locate a control in a hierarchy and to give the control a meaningful identifier to interact with the specific instance of the control.
Upvotes: 3
Reputation: 5566
It is still unique as each Object. It inherits from Object.
To see the Hashcode with which you can also compare to another Object you can use following code:
example XAML:
<Grid Name="BaseGrid">
<Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="73,34,0,0" VerticalAlignment="Top" Width="75" />
<Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="73,101,0,0" VerticalAlignment="Top" Width="75" />
</Grid>
code to visualise the HashCode:
foreach (UIElement child in BaseGrid.Children)
{
MessageBox.Show(child.GetHashCode().ToString());
}
but to come back to your question "what determines its identity in relationship to other controls": Every object is a pointer to an area of memory on the heap. The XAML just tells the compiler how to load the objects. after they are loaded they are referenced by the memory address.
Upvotes: 3
Reputation: 100527
Controls have position in its parent's child list - so every control can be identified by starting from top element and getting particular child that contains control you are interested in.
Please check out this answer - How can I find WPF controls by name or type? for details.
Upvotes: 1