1

I'm getting a very unusual error on a project that use to work where I'm trying to bind an ENUM to a combo box. To ensure I've not made coding errors, I've made a new usercontrol using SO Question 58743 and ageektrapped as samples for a self contained user control. I'm using .Net4 Client Framework as the environment and VS2010. The xaml is -

<UserControl x:Class="Barcode.Views.UserControl1" 
             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:System="clr-namespace:System;assembly=mscorlib"  
             mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="300">

    <UserControl.Resources>
        <ObjectDataProvider MethodName="GetValues" 
                            ObjectType="{x:Type System:Enum}" 
                            x:Key="AlignmentValues">
            <ObjectDataProvider.MethodParameters>
                <x:Type TypeName="HorizontalAlignment" />
            </ObjectDataProvider.MethodParameters>
        </ObjectDataProvider>
    </UserControl.Resources>
    <Grid>
        <ComboBox Name="myComboBox" SelectedIndex="0" Margin="8" 
                  ItemsSource="{Binding Source={StaticResource AlignmentValues}}" />
    </Grid>
</UserControl>

The error that I'm getting on the ComboBox is Error 144 Unable to cast object of type 'System.String' to type 'System.Windows.DataTemplate' after building the project.

I'm at a loss as to what could be causing this error.

Community
  • 1
  • 1
photo_tom
  • 7,292
  • 14
  • 68
  • 116

1 Answers1

1

Looks like you may be missing the namespace on your HorizontalAlignment. Add a relevant namespace where the HorizontalAlignment type resides.

xmlns:local="clr-namespace:Barcode.Views"

Then modify your XAML to make use of the newly defined namespace...

...

<x:Type TypeName="local:HorizontalAlignment"/>

...

EDIT:

With this being the HorizontalAlignment enum type from within the framework then your code should work as is. I tested it to be certain and it indeed works; as I placed an instance of the UserControl on my Window and it worked without a hitch. Set up an empty project and start from scratch to see if the problem still exists as you may have other factors causing the issue.

Aaron McIver
  • 24,527
  • 5
  • 59
  • 88
  • Actually for this demo, I was trying to get the Horizontal alignment enums (left, right, center) as a know enum that works. This is how many of the demos have been written. – photo_tom Jan 21 '11 at 17:17
  • @photo_tom Added addition commentary, just copied/pasted your code and it does indeed work – Aaron McIver Jan 21 '11 at 17:24
  • @Aaron - That what is I've seen. However, in this project and another new project, I cannot get the code to run without the error. – photo_tom Jan 21 '11 at 17:31
  • @photo_tom So you went to add new UserControl and selected WPF UserControl (not WinForms) within the IDE then copied/pasted the above code and changed the x:Class for the UserControl and it will not compile? – Aaron McIver Jan 21 '11 at 17:45
  • @Aaron - After compile, the combo box is reporting being in an error state. If I go to run it, I will be a XAML parsing error. But I just tried building a completely new solution and it works there. There must be something corrupt in my solution, but I cannot figure out what. – photo_tom Jan 21 '11 at 18:00
  • @photo_tom Could be global templates within your App.xaml or other embedded resource. Make sure you have all your varying templates commented out in some fashion and see what happens. – Aaron McIver Jan 21 '11 at 18:04
  • @Aaron - That was it. There is a problem with global template. Thanks. – photo_tom Jan 21 '11 at 18:09