Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Binding ComboBox to enum
Message
From
18/12/2012 09:29:12
 
 
To
18/12/2012 08:48:42
General information
Forum:
ASP.NET
Category:
Windows Presentation Foundation (WPF)
Miscellaneous
Thread ID:
01559922
Message ID:
01560005
Views:
70
>Viv,
>
>I'm trying to understand this one:
>
>>Or using ObjectDataProvider:
>>http://www.codeproject.com/Articles/29495/Binding-and-Using-Friendly-Enums-in-WPF
>>
>
>I hope you can help me work it out.
>
>I have defined my enum in my business object class:
>
>
namespace SamaanSystems.IBC.Business
>{
>	/// <summary>
>	/// Summary description for Rate.
>	/// </summary>
>	public partial class Rate
>	{
>         public enum RateType : int
>         {
>            Standard = 1,
>            Mail = 2,
>            ZipX = 3
>         }
>	}
>}
>
>I am trying to have this enum be used to display the possible options on my Rates form in a combo, so the user will see Standard, Mail and ZipX as the items in the combo list and the values of 1, 2 and 3 respectively will be bound to a property of an entity object. The field is rat_type.
>
>I've stuck this code in my RatesWindow.xaml:
>
>
    <ObjectDataProvider x:Key="rateTypes"
>                    MethodName="GetValues" 
>                    ObjectType="{x:Type sys:Enum}">
>        <ObjectDataProvider.MethodParameters>
>            <x:Type TypeName="local:RateTypes" />
>        </ObjectDataProvider.MethodParameters>
>    </ObjectDataProvider>
>
>but I get a squiggly blue underline under x:Key="rateTypes" with this error:
>
>Key attribute can be used only on a tag contained in an IDictionary type property
>
>This also cause almost the entire rest of my xaml to be squiggly underlined in blue with this error:
>
>The property 'Content' is set more than once.
>
>So I guess I'm putting this in the wrong place. This is the first bit of my xaml:
>
>
<local:ABusinessWindow
>	xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
>	xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
>	xmlns:mmwpf="clr-namespace:OakLeaf.MM.Main.WPF;assembly=OakLeaf.MM2010.WPF"
>	xmlns:d="http://schemas.microsoft.com/expression/blend/2006" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"
>	xmlns:Custom="http://schemas.microsoft.com/wpf/2008/toolkit"
>	xmlns:SamaanSystems_IBC_Business="clr-namespace:SamaanSystems.IBC.Business;assembly=SamaanSystems.IBC.Business"
>	xmlns:local="clr-namespace:SamaanSystems.IBC.WPF"
>    x:Class="SamaanSystems.IBC.WPF.RatesWindow"
>	x:Name="Window"
>	Title="Rates"
>	Width="550" Height="450" AllowsTransparency="False" Background="#FFFFFFFF" WindowStartupLocation="CenterScreen" >
>
>    <ObjectDataProvider x:Key="rateTypes"
>                    MethodName="GetValues" 
>                    ObjectType="{x:Type sys:Enum}">
>        <ObjectDataProvider.MethodParameters>
>            <x:Type TypeName="local:RateTypes" />
>        </ObjectDataProvider.MethodParameters>
>    </ObjectDataProvider> 
>    
>    <mmwpf:mmBusinessWindow.DataContext>
>        <SamaanSystems_IBC_Business:RateEntity/>
>    </mmwpf:mmBusinessWindow.DataContext>
>
>    <Border>
>        <Border.Background>
> ...
>
>Can you explain this a bit for me and tell me where the code should be going?
>
>Thanks very much!

I think the main problem is that the ObjectDataProvider must be in Windows.Resources. Something like this:
<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local ="clr-namespace:WpfApplication1"
        xmlns:sys="clr-namespace:System;assembly=mscorlib"
        Title="MainWindow" Height="350" Width="525">
<Window.Resources>
    <ObjectDataProvider x:Key="rates"
                    MethodName="GetValues" 
                    ObjectType="{x:Type sys:Enum}">
      <ObjectDataProvider.MethodParameters>
        <x:Type TypeName="local:RateType" />
      </ObjectDataProvider.MethodParameters>
    </ObjectDataProvider>
  </Window.Resources>
<ComboBox ItemsSource="{Binding Source={StaticResource rates}}"></ComboBox>  
</Window>
I just tried this with RateType defined in the same window so you'll need to adjust.

UPDATE: Should have said " with RateType defined in the same namespace as the window"

HTH,
Viv
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform