开发者

Databinding Enum to ComboBox

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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜