2012-04-23 67 views
1

为了绑定一个“友好的”枚举我请点击此链接WPF数据绑定友好枚举错误

Databinding an enum property to a ComboBox in WPF

但我有此错误:无法从“状态”字符串创建“类型”

这背后

public enum Status 
    { 
     [Description("Available.")] 
     Available, 
     [Description("Not here right now.")] 
     Away, 
     [Description("I don't have time right now.")] 
     Busy 
    } 


    public Status CurrentStatus { get; set; } 


    public MainWindow() 
    { 
     InitializeComponent(); 

    } 

我的代码,这是我的XAML

<Grid> 
    <ComboBox 
     ItemsSource="{Binding Source={my:Enumeration {x:Type Status}}}" 
     DisplayMemberPath="Description" 
     SelectedValue="{Binding CurrentStatus}" 
     SelectedValuePath="Value" /> 

</Grid> 

什么是我错了?

谢谢

回答

0

您缺少名称空间。如果你的代码是在一个名为MyProject命名空间,那么你需要参考你的XAML文件的顶部添加到:

<xmlns:proj="clr-namespace:MyProject" /> 

,然后相应前缀的类型:

ItemsSource="{Binding Source={my:Enumeration {x:Type proj:Status}}}" 

编辑:期待在您现有的标记中,使用my:Status可能就足够了。

+0

对不起我有:xmlns:my =“clr-namespace:WpfApplication3”..and ItemsSource =“{Binding Source = {my:Enumeration {x:Type my:Status}}}”但错误是一样的 – user1351709 2012-04-23 16:07:10

+0

在哪个命名空间中定义了你的类型'Status'? – 2012-04-23 16:10:08

+0

WpfApplication3 – user1351709 2012-04-23 16:13:30