Sergej Andrejev
Sergej Andrejev

Reputation: 9403

Strange WPF errors in Visual Studio

Does anybody know of the reason why ""could not create instance of UserControl"" error can occur. It seems to me that it occurs completely spontanous, for example after I add space after node or change tabulation. Maybe it's some kind of VS bug?

Here are controls. This configuration actually raises an error

<UserControl x:Class="ShortcutsPrototype.KeyboardShortcutsTreePanel"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:ShortcutsPrototype="clr-namespace:ShortcutsPrototype">


    <Grid Margin="3,3,3,3">
        <Grid.ColumnDefinitions>
            <ColumnDefinition />
            <ColumnDefinition Width="80" />
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition />
            <RowDefinition Height="30" />
        </Grid.RowDefinitions>


        <ShortcutsPrototype:KeyboardShortcutsTreeView />
        <Button Grid.Row="1" Margin="3,3,3,3" Grid.Column="1" HorizontalAlignment="Stretch">Reset</Button>
    </Grid>
</UserControl>


<UserControl x:Class="ShortcutsPrototype.KeyboardShortcutsTreeView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:ShortcutsPrototype="clr-namespace:ShortcutsPrototype"
    Height="300" Width="300">

    <Grid>
        <TreeView>
            <TreeViewItem HorizontalContentAlignment="Stretch">
                <TreeViewItem.Header>
                    <ShortcutsPrototype:KeyboardShortcutsTreeViewEntry x:Name="generalTreeViewEntry" Title="General" />
                </TreeViewItem.Header>

                <TreeViewItem HorizontalContentAlignment="Stretch">
                    <TreeViewItem.Header>
                        <ShortcutsPrototype:KeyboardShortcutsTreeViewEntry x:Name="generalNewFileTreeViewEntry" Title="New File" ShortcutKey="Ctrl+N"  />
                    </TreeViewItem.Header>
                </TreeViewItem>

                <TreeViewItem HorizontalContentAlignment="Stretch">
                    <TreeViewItem.Header>
                        <ShortcutsPrototype:KeyboardShortcutsTreeViewEntry x:Name="generalOpenFileTreeViewEntry" Title="Open File" ShortcutKey="Ctrl+O"  />
                    </TreeViewItem.Header>
                </TreeViewItem>

                <TreeViewItem HorizontalContentAlignment="Stretch">
                    <TreeViewItem.Header>
                        <ShortcutsPrototype:KeyboardShortcutsTreeViewEntry x:Name="generalSaveFileTreeViewEntry" Title="Save File" ShortcutKey="Ctrl+S"  />
                    </TreeViewItem.Header>
                </TreeViewItem>

                <TreeViewItem HorizontalContentAlignment="Stretch">
                    <TreeViewItem.Header>
                        <ShortcutsPrototype:KeyboardShortcutsTreeViewEntry x:Name="generalSaveAsFileTreeViewEntry" Title="Save File As.." ShortcutKey="Ctrl+Shift+S"  />
                    </TreeViewItem.Header>
                </TreeViewItem>

            </TreeViewItem>
            <TreeViewItem HorizontalContentAlignment="Stretch">
                <TreeViewItem.Header>
                    <ShortcutsPrototype:KeyboardShortcutsTreeViewEntry x:Name="debugerTreeViewEntry" Title="Debuger" />
                </TreeViewItem.Header>
            </TreeViewItem>
            <TreeViewItem HorizontalContentAlignment="Stretch">
                <TreeViewItem.Header>
                    <ShortcutsPrototype:KeyboardShortcutsTreeViewEntry x:Name="refactoringTreeViewEntry" Title="Refactoring" />
                </TreeViewItem.Header>
            </TreeViewItem>
        </TreeView>
    </Grid>
</UserControl>



<UserControl x:Class="ShortcutsPrototype.KeyboardShortcutsTreeViewEntry"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Height="25">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition  />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition />
            <ColumnDefinition Width="100" />
        </Grid.ColumnDefinitions>
        <Label x:Name="titleLabel" Grid.Column="0"></Label>
        <Label x:Name="shortcutLabel" Grid.Column="1"></Label>
    </Grid>
</UserControl>

Upvotes: 0

Views: 265

Answers (2)

Alan Jackson
Alan Jackson

Reputation: 6511

I have found that visual studio has quite the difficult time with WPF user controls.

The only thing that I can see is that it seems to use the latest compiled version of a control. Usually, building the solution will fix the errors I have. Sometimes, if I have multiple projects I have to build them manually one by one.

Upvotes: 0

Orion Edwards
Orion Edwards

Reputation: 123612

Perhaps something in your CodeBehind file is throwing an exception. Have you checked that?

Upvotes: 1

Related Questions