Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Binding ComboBox to enum
Message
De
18/12/2012 13:42:00
 
 
À
18/12/2012 11:57:39
Information générale
Forum:
ASP.NET
Catégorie:
Windows Presentation Foundation (WPF)
Divers
Thread ID:
01559922
Message ID:
01560057
Vues:
53
>>>Of course, if there's nothing else in Rate then the class is redundant.....
>>
>>There is more to the class, I just cut it out when pasting here to avoid confusion. I had tried it in a separate file with the same namespace and got it to work, but I'll put it back in there as you described as I feel it makes more sense to me there.
>>
>>Now, how do I get it to actually display the correct value and save it back to my database when a change is made? (My framework will handle all the saving automatically, I just need to get it to bind properly.)
>>
>>So here is my xaml for the combo:
>>
>>
<ComboBox Name="cboRateTypes" SelectedValue="{Binding rat_type, Mode=Default}" ItemsSource="{Binding Source={StaticResource rateTypes}}"></ComboBox>
>>
>>Is there something else I need to set in the definition?
>
>Looks OK to me. Should work assuming rat_type is a valid property in the DataContext. FWIW I tested 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}}" SelectedValue="{Binding SelectedRate}"></ComboBox>  
></Window>
and
using System.Windows;
>
>namespace WpfApplication1
>{
>  public partial class MainWindow : Window
>  {
>    public MainWindow()
>    {
>      InitializeComponent();
>      DataContext = this;
>    }
>
>    private RateType _selectedRate;
>    public RateType SelectedRate
>    {
>      get { return _selectedRate; }
>      set { _selectedRate = value; }
>    }
>  }
>
>  public enum RateType : int
>  {
>    Standard = 1,
>    Mail = 2,
>    ZipX = 3
>  }
>
>}
Hmm...

This is proving to be more trouble than it's worth. Maybe I'll go back to using a table rather than an enum. At least that's easy.

What is happening is that the combo is populated with the enum values, but it does not display the appropriate item from the list when I select an existing record. Is there some sort of code that I'll have to add in to cause it refresh or display the correct one?
Frank.

Frank Cazabon
Samaan Systems Ltd.
www.samaansystems.com
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform