2016-11-08 103 views
1

我在WPF自定义的控制,我需要一个ComboBox它绑定到一个枚举我写的,绑定枚举组合框上的自定义控制

搜索,我发现,这是要走的路在网络上:

<ObjectDataProvider 
    MethodName="GetDict" 
    ObjectType="{x:Type App:EnumDescriptionValueDict}" 
    x:Key="EnumDescriptionDict"> 
    <ObjectDataProvider.MethodParameters> 
    <x:Type TypeName="App:Transmission"></x:Type> 
    </ObjectDataProvider.MethodParameters> 
</ObjectDataProvider> 

<ComboBox 
     ItemsSource="{Binding Source={StaticResource EnumDescriptionDict}}" 
     DisplayMemberPath="Key" 
     SelectedValuePath="Value"/> 

但我的控制XAML

<UserControl x:Class="WpfControlFoo.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" 
      mc:Ignorable="d" Width="799" Height="107"> 

,所以我不觉得插入ObjectDataProvider的 XAML

0的地方

感谢您的建议:)

+1

使用区域来定义它 –

+1

Evk

回答

2

您可以按照评论中的建议使用资源。

全XAML:

<UserControl x:Class="WpfControlFoo.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:App="myNamespaceWhereTheEnumIsLocated" 
      mc:Ignorable="d" Width="799" Height="107"> 
<UserControl.Resources> 
<ObjectDataProvider 
    MethodName="GetDict" 
    ObjectType="{x:Type App:EnumDescriptionValueDict}" 
    x:Key="EnumDescriptionDict"> 
    <ObjectDataProvider.MethodParameters> 
    <x:Type TypeName="App:Transmission"></x:Type> 
    </ObjectDataProvider.MethodParameters> 
</ObjectDataProvider> 
</UserControl.Resources> 
<ComboBox 
     ItemsSource="{Binding Source={StaticResource EnumDescriptionDict}}" 
     DisplayMemberPath="Key" 
     SelectedValuePath="Value"/> 
</UserControl> 
+0

而只是为了增加@Mat answer,'App:'是公共枚举所在的命名空间。 –

+0

@LupuSilviu thx指出了这一点。添加到我的文章 – Mat