1

I've got the enum like so.

enum Beep {A, B, C }

Now, I wish to populate my combo box with these values as follows (the idea is to follow other's example).

<ComboBox x:Name="comboBox1" 
          ...
          ItemsSource="Binding Source={StaticResource Beep}" />

However, the binding gets a little bit too static and gives me literally the exact string I'm putting in. What did I do wrong and how do I resolve it?

enter image description here

I've also tried following the hint I got to add something like this. To no avail, though.

public List<Beep> Beepies
{
  get
  {
    return new List<Beep>{ Beep.A }
  }
}

What more can be done about it? I can get the values into the box if I bind in the code behind by the below. But that's not the point - I wish to XAMLize the approach.

comboBox1.ItemsSource = Enum.GetValues(typeof(Beep));
Community
  • 1
  • 1
Konrad Viltersten
  • 36,151
  • 76
  • 250
  • 438
  • @Rainer Please consider reading more carefully the contents of both the questions, not just flag happily as you see that there's something linked. The questions are fundamentally different. They just use the same set of keywords because they're from the same area. – Konrad Viltersten Jan 01 '15 at 18:19
  • Please consider that your question directed me in the given direction. Since you can assume, that my flagging is not bad will, it leeds to the conclusion, that your question can be misunderstood. To your problem: could it be, that your are just doing the databinding wrong? Instead of `` you should probably use `` ? – Rainer Jan 02 '15 at 03:54
  • you are missing `{`. use ItemsSource="{Binding Source={StaticResource Beep}}" – Vimal CK Jan 02 '15 at 06:53
  • @Rainer I don't think the cause of the wrong flagging was **bad will**. I think it was laziness and a sloppy check. That's why I politely asked you to, please, check the actual contents and intention of both questions. You're right - the question apparently **could** be misunderstood. However, it might be also be understood correctly (since I got my answer). So the issue is **why** it was misunderstood by you in this particular case. Could be a gazillion of reasons, no point discussing it. Also, check the comment by Vimal. His hint is very clear and to the point. Thanks anyway and happy new y! – Konrad Viltersten Jan 02 '15 at 15:48
  • @VimalCK Spot on, mate! Good eyes. I also was missing *}*, hehe. Thanks! Put that as a reply so I can give you some rep. – Konrad Viltersten Jan 02 '15 at 15:49

1 Answers1

0

You have to bind using a collection. Try creating a List<Beep>.

OR

Create a converter that enumerates the enum:

public class EnumConverter : IValueConverter
{
  public object Convert(object value, ...)
  {
    return Enum.GetValues(value.GetType());
  }
}

You can then use this converter in the binding to XAMLise the approach:

<!-- Put this in resources somewhere -->
<Converters:EnumConverter x:Key="MyEnumConverter"/>

<!-- Change your binding to include the new converter -->
<ComboBox x:Name="comboBox1" 
          ...
          ItemsSource="Binding Source={StaticResource Beep}, 
                       Converter={StaticResource MyEnumConverter}" />
Laith
  • 6,071
  • 1
  • 33
  • 60
  • I've made an addition to the question. Suggestions on that? – Konrad Viltersten Jan 01 '15 at 16:18
  • I added another approach using a converter. This will be reusable for any `enum` – Laith Jan 02 '15 at 00:41
  • It seems not to work (got some errors about names not being available. However, when I used: *ItemsSource="{x:Static local:MainWindow.AllBeeps}"*, it seems to work. Can you confirm that it's just a different syntax for what you're suggesting, please? I'm not sure what the *x* does there. And I'd like to know if the *local:ClassName.PropName* can be made shorter by using some kind of reference higher up in the XAML code. – Konrad Viltersten Jan 02 '15 at 15:24