1

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 :)

2
  • 1
    Use the <UserControl.Resources> area to define it in. Commented Nov 8, 2016 at 8:47
  • 1
    <UserControl.Resources><ObjectDataProvider ...></UserControl.Resources> Commented Nov 8, 2016 at 8:47

1 Answer 1

3

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> 
Sign up to request clarification or add additional context in comments.

1 Comment

And just to add to @Mat answer, App: is the namespace where you public enum is located.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.