Wpf 组合框将项目绑定到枚举项目

Wpf combobox binding items to rthe enum items

我试图将我的枚举中的项目绑定到我的组合框,但它不起作用 我的项目看起来像这样 解决方案

<Window x:Class="Telefoon.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:System="clr-namespace:System;assembly=mscorlib"        
        xmlns:local="clr-namespace:Telefoon"
        xmlns:models="clr-namespace:Telefoon.Models"
        mc:Ignorable="d"
        DataContext="Persoon"
        Title="MainWindow" Height="450" Width="800">
    <Window.Resources>
        <ObjectDataProvider x:Key="dataFromEnum" MethodName="GetValues"
                            ObjectType="{x:Type System:Enum}">
            <ObjectDataProvider.MethodParameters>
                <x:Type TypeName="models:Groep"/>
            </ObjectDataProvider.MethodParameters>
        </ObjectDataProvider>
    </Window.Resources>
    <Grid>
        <ComboBox ItemsSource="{Binding Source={StaticResource dataFromEnum}}">
            
        </ComboBox>
    </Grid>
</Window>
´´´

这应该有效!刚刚在一个简单的演示应用程序中尝试了您的方法,一切正常。重建应用程序,重新启动 Visual Studio,检查命名空间引用!如果你放入一个新的命名空间并在没有先重建的情况下引用它,VS 总是会报错,但我认为这不是你的问题。

这是我的演示代码

<Window x:Class="WpfApp1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:sys="clr-namespace:System;assembly=mscorlib"
    xmlns:local="clr-namespace:WpfApp1"
    mc:Ignorable="d"
    Title="MainWindow" Height="450" Width="800">
<Window.Resources>
    <ObjectDataProvider x:Key="GroepData" 
                        MethodName="GetValues" 
                        ObjectType="{x:Type sys:Enum}">
        <ObjectDataProvider.MethodParameters>
            <x:Type TypeName="local:Groep"/>
        </ObjectDataProvider.MethodParameters>
    </ObjectDataProvider>
</Window.Resources>

<Grid HorizontalAlignment="Center" VerticalAlignment="Top" Margin="20">
    <ComboBox ItemsSource="{Binding Source={StaticResource GroepData}}" Width="200">
        
    </ComboBox>

</Grid>

另一种(更清洁的)方法来自 Brian Lagunas。他使用了可以在 XAML 中引用的自定义标记扩展。这对于您项目中的所有枚举也是完全可重用的,而且我真的很喜欢为这样的问题简单地创建您自己的 MarkupExtension 的想法。

自定义标记扩展只是一个单独的 class,继承自 MarkupExtion。

public class EnumBindingSourceExtension : MarkupExtension
{
    public Type EnumType { get; private set; }

    public EnumBindingSourceExtension(Type enumType)
    {
        if (enumType is null || !enumType.IsEnum)
            throw new Exception("Type null or not an enum");
        else
            EnumType = enumType;
    }

    public override object ProvideValue(IServiceProvider serviceProvider)
    {
        return Enum.GetValues(EnumType);
    }
}

您可以在设置组合框的 ItemsSource-Binding 时简单地使用该标记扩展。

<ComboBox ItemsSource="{Binding Source={local:EnumBindingSource {x:Type local:Groep}}}"/>

希望对您有所帮助。

你好,帕科