Question: In our WPF software, we used a `ControlTemplate` which defines a `ToggleButton` that causes the window to shrink/extend. The definition of `ToggleButton` is given below:

```xml
<ToggleButton ToolTip="Standard/Extended" Grid.Column="0"
    x:Name="PART_MaximizeToggle" VerticalAlignment="Top"
    HorizontalAlignment="Right" Margin="0,5,5,0"
    Width="14" Height="14" Cursor="Hand">

```

We're creating a custom `DockPanel` which contains this button at the upper right corner. Our application can contain up to three of this `DockPanel`s at the same time:

[![DockPanel screenshots](https://i.stack.imgur.com/FFsTH.png)](https://i.stack.imgur.com/FFsTH.png)

The small rectangle on the right of each `DockPanel` is shown in the image above.

Notice from the definition that all three of the rectangles have same name: `"PART_MaximizeToggle"`. This causes trouble when writing CodedUI programs to automate testing. CodedUI captures all of their FriendlyNames as `"PART_MaximizeToggle"` with Name field empty. The locations and sequence of the `DockPanel`s can change based or what the user want.

How can we make CodedUI capture exact the button where a click is expected? I was thinking of making the `Name` of each toggle button dynamic but fixed for a specific `DockPanel`. 

How can I do that? Is there a better approach?

Answer: 
You could assign ([and register](https://msdn.microsoft.com/en-us/library/system.windows.frameworkcontentelement.registername.aspx)) the names automatically via an [AttachedProperty](https://msdn.microsoft.com/en-us/library/ms749011(v=vs.110).aspx) that increments a counter for each prefix.
(This is just a proof of concept, you should also check that the names are valid)

```
public static class TestingProperties
{
    private static readonly Dictionary<string, int> _counter = new Dictionary<string, int>();

    public static readonly DependencyProperty AutoNameProperty = DependencyProperty.RegisterAttached(
        "AutoName", typeof(string), typeof(TestingProperties), new PropertyMetadata(default(string), OnAutoNamePropertyChanged));

    private static void OnAutoNamePropertyChanged(DependencyObject element, DependencyPropertyChangedEventArgs eventArgs)
    {
        string value = (string) eventArgs.NewValue;

        if (String.IsNullOrWhiteSpace(value)) return;
        if (DesignerProperties.GetIsInDesignMode(element)) return;
        if (!(element is FrameworkElement)) return;

        int index = 0;
        if (!_counter.ContainsKey(value))
            _counter.Add(value, index);
        else
            index = ++_counter[value];

        string name = String.Format("{0}_{1}", value, index);
        ((FrameworkElement)element).Name = name;
        ((FrameworkElement)element).RegisterName(name, element);
    }

    public static void SetAutoName(DependencyObject element, string value)
    {
        element.SetValue(AutoNameProperty, value);
    }

    public static string GetAutoName(DependencyObject element)
    {
        return (string)element.GetValue(AutoNameProperty);
    }
}

```

Usage in XAML:

```xml
<!-- will be Button_0 -->
<Button namespace:TestingProperties.AutoName="Button"/>

<!-- will be Button_1 -->
<Button namespace:TestingProperties.AutoName="Button"/>

<!-- will be Button_2 -->
<Button namespace:TestingProperties.AutoName="Button"/>

```

Resulting Visual-Tree:

![VisualTree of Example](https://i.stack.imgur.com/sKahi.jpg)
Manfred Radlwimmer's solution is useful but makes the controls code behind harder. 

Any dynamic code in the Controls' OnApplyTemplate that searches for that template part will become a pain.

An alternative would be to use same trick (generation of a unique id) for the automation id instead and use the automation id in the tests.

See:
<http://www.jonathanantoine.com/2011/11/03/coded-ui-tests-automationid-or-how-to-find-the-chose-one-control/>