开发者

Bind enum from external class in same namespace to ListBox (or whatever) in WPF (C# and XAML)

I have looked and looked, but nothing works.

Say I have something like this:

namespace A{

    partial class B : Window {
        //some definitions
    }

    class E {
        public enum en {a, b, c}
    }
}

and then in the XAML:

<Window x:Class="A.B"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:A">
<Window.Resources>
        <ObjectDataProvider MethodName="GetValues"
                        ObjectType="{x:Type sys:Enum}"
                        x:Key="vals">
            <ObjectDataProvider.MethodParameters>
                <x:Type Type开发者_Python百科Name="en" />           <<<this line
            </ObjectDataProvider.MethodParameters>
        </ObjectDataProvider>
    </Window.Resources>
..rest of XAML ...
</window>

Now the marked line gives me the error:

Type 'en' was not found.

it is the same if I change it to

local:en
E+en
local:E+en

How do I solve this problem? Thanks very much


just try this

<Window x:Class="WpfApplication12.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication12"
    xmlns:System="clr-namespace:System;assembly=mscorlib"
Title="Window1" Height="300" Width="300">
<Window.Resources>
    <ObjectDataProvider x:Key="dataFromEnum"
                        MethodName="GetValues"
                        ObjectType="{x:Type System:Enum}">
        <ObjectDataProvider.MethodParameters>
            <x:Type TypeName="local:Window1+en" />
        </ObjectDataProvider.MethodParameters>
    </ObjectDataProvider>
</Window.Resources>
<Grid>
    <ListView ItemsSource="{Binding Source={StaticResource dataFromEnum}}"
              Margin="10,10,10,0"
              Height="80"
              VerticalAlignment="Top" />
    <ComboBox ItemsSource="{Binding Source={StaticResource dataFromEnum}}"
              Margin="10,0,10,80"
              Height="25"
              VerticalAlignment="Bottom" />
    <ListBox ItemsSource="{Binding Source={StaticResource dataFromEnum}}"
              Margin="10,0,10,12"
              Height="93"
              VerticalAlignment="Bottom" />
</Grid>

public partial class Window1 : Window
{
    public Window1()
    {
        InitializeComponent();
    }
    public enum en { a, b, c }

}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜