简体   繁体   中英

bind enum to Combobox On CUSTOM CONTROL

I have a Custom control in WPF and I need to bind a comboBox on it to an enum I wrote,

searching on the web I found that this is the way to go:

<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"/>

but my control 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">

so I don't find a place to insert the ObjectDataProvider XAML

Thanks for the suggestions :)

You can use Resources as suggested in comments.

full 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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM