I have a large WPF solution running around for 2 years. Now we're running an automated build environment for that solution when the strangest thing happened.

In 50% of our builds, I get this error:

Exception: Unable to cast object of type 'System.Windows.Controls.StackPanel' to type 'System.Windows.Controls.Border'. Error at object 'System.Windows.Controls.StackPanel' in markup file ...

It seems simple enough. The problem is that my code behind is the following:

<UserControl x:Class="SiSM.Episode.Mishap.SpecializationList" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:Converters="clr-namespace:Utils.Converters;assembly=Utils" ...>
    <Border x:Name="root"  BorderThickness="0.5">
        <StackPanel x:Name="stackPanelRoot" VerticalAlignment="Stretch">
            <Grid>
                ...
            </Grid>
            <StackPanel>
                ...
            </StackPanel>
            <ScrollViewer>
                ...
            </ScrollViewer>
        </StackPanel>
    </Border>
</UserControl>

The error is here because if I switch the stackpanel for a dockpanel the error message changed to a dockpanel.

My build environment is the following:

Copy the code to a build folder:

private void CopyCode(string sourceDir, string destinationDir) {
            foreach (string dirPath in Directory.GetDirectories(sourceDir, "*", SearchOption.AllDirectories)) {
                if (!dirPath.Contains(".svn") && !dirPath.Contains(@"\bin") && !dirPath.Contains(@"\obj")) {
                    Directory.CreateDirectory(dirPath.Replace(sourceDir, destinationDir));
                }
            }

            foreach (string newPath in Directory.GetFiles(sourceD
Edit
Report