wpfmvvmnamespacesbehaviorvisualstates

WPF X:Name causes type name 'ViewModel' Compile Error


I intend to set a state of CalibrationGridControl from a button. To do that, the CalibrationGridControl UserControl must have a x:Name (Blend even adds one for me when I setup the the GoToState behavior. Problem is, as soon as I add x:Name="calibrationGridControl" I get the following compile error.

Error CS0426 The type name 'ViewModel' does not exist in the type 'TeachpendantControl' TeachPendantControl C:\GitRepos\SolutionName\TheWPFControl\Views\HandeyeCalibration\HandeyeCalibrationView.xaml 150 38 Build Active Compiler

The UserControl HandeyeCalibrationView below is a View to be shown inside a ContentControl in the "TheWPFControl". TheWPFControl and the HandyeCalibration.xaml are both in the same project (a WPF Control library). Below is the essential part of the HandeyeCalibration.xaml file where I get the error.

<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vm="clr-namespace:TeachpendantControl.ViewModel"
xmlns:local="clr-namespace:TeachpendantControl.Views"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
xmlns:b="clr-namespace:Wpf.Behaviours"
xmlns:HandeyeCalibration="clr-namespace:TeachpendantControl.ViewModel.H"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" 
xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"
x:Class="TeachpendantControl.Views.HandeyeCalibrationView" 
mc:Ignorable="d"  
d:DataContext ="{d:DesignInstance {x:Type vm:HandeyeCalibrationViewModel}, 
IsDesignTimeCreatable=True}" 
Height="111.221" 
Width="276.813"
>
<UserControl.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="../../ResourceDictionary.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</UserControl.Resources>
<UserControl.DataContext>
    <Binding Mode="OneWay" 
             Path="HandeyeCalibrationViewModel" 
             Source="{StaticResource Locator2}"/>
</UserControl.DataContext>
 <Grid> 
 <UserControl>
...

          <HandeyeCalibration:CalibrationGridControl 
            x:Name="calibrationGridControl"                                             
            HorizontalAlignment="Left" 
            Margin="0" 
            VerticalAlignment="Top" 
            Height="106" 
            Width="106"                
            Background="#FF747474"/>    
     <Button Command="{Binding AddCommand}" 
           Content="{Binding AddText, UpdateSourceTrigger=PropertyChanged}" 
           Margin="0,0,5,0">
              <i:Interaction.Triggers>
                 <i:EventTrigger EventName="Click">
                   <ei:GoToStateAction TargetName="calibrationGridControl" 
                      StateName="{Binding NextPositionState, Mode=OneWay}"/>
                   </i:EventTrigger>
              </i:Interaction.Triggers>                   
     </Button>
</Grid>
</UserControl>

If I only remove the x:Name="calibrationGridControl" line everything compiles just fine. What could be wrong, causing this strange error message?

I got some questions about the CalibrationGridControl. If I just remove the x:Name when adding it to another user control everything compiles. The XAML for the CalibrationGridControl looks like this.

<UserControl x:Class="TeachpendantControl.ViewModel.H.CalibrationGridControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:TeachpendantControl.ViewModel"
             xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity;assembly=System.Windows.Interactivity"             
             xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <UserControl.Resources>
        <SolidColorBrush x:Key="CalibrationGridCrossBrush" Color="#FFFFDF00"/>
        <SolidColorBrush x:Key="CalibrationGridPositionTrainedFillBrush" Color="#FFFFDF00"/>
        <SolidColorBrush x:Key="CalibrationGridPositionFillBrush" Color="Black"/>
    </UserControl.Resources>
    <Grid>        
        ...            
    </Grid>
</UserControl>

Solution

  • TeachpendantControl apparently is both a type and a namespace.

    You should either change the name of the control or the change the name of the namespace to avoid a naming collision.