You need to create a data source for the ComboBox. An instance of the following class is set as the ItemsSource (the insignificant fields are omitted):

public class TemperatureDimension : IDimension, IEnumerable { private List<IUnit> _Units; public List<IUnit> Units { get { return _Units; } } public TemperatureDimension() { _Units =new List<IUnit>{ new Celsius(), new Kelvin(), new Farenheit() }; } public IEnumerator GetEnumerator() { return _Units.GetEnumerator(); } } 

In principle, it is logical that this code does not work and the ComboBox is not populated with data. I see the following solutions to this problem:

1) Write something like

 <ComboBox Width="100" Height="30" ItemsSource="{Binding Dimension.Units}" /> 

2) Correct the code so that the class is a collection containing Items. I tried to solve this problem by implementing the IEnumerable interface, returning a list enumerator in the GetEnumerator method. But to no avail.

Any ideas?

UPD. I also tried to implement the IEnumerable <T> interface. In this case it is not compiled.

  • It would be worth pointing out tags like .Net, C # ... - free_ze
  • Try not IEnumerable , but IEnumerable<IUnit> . What exactly is not compiled in this case? - VladD
  • And what's the problem with {Binding Dimension.Units} ? It seems everything is correct. What writes to the log (Output window) about Binding errors? - VladD
  • @VladD, when I try to implement the IEnumerable interface <IUnit>, I write: public IEnumerator <IUnit> GetEnumerator () {return _Units.GetEnumerator (); } does not compile. This error: Calc.MeasuringSystem.TemperatureDimension does not implement the interface member "System.Collections.IEnumerable.GetEnumerator ()". "Calc.MeasuringSystem.TemperatureDimension.GetEnumerator ()" cannot be implemented by "System.Collections.IEnumerable.GetEnumerator ()" because it does not contain the appropriate return type "System.Collections.IEnumerator". How to do? - maestro
  • @VladD, with {Binding Dimension.Units} ComboBox is not populated with data. Resource 100% exists (watched it in the debugger). In the debugger, I also noticed that the get field method Units is not called. - maestro

1 answer 1

Everything works for me (c).

Probably you

  1. Did not declare Dimension as a property, but only as a field?
  2. IEnumerable<IUnit> implementing IEnumerable<IUnit> , they forgot about one of the GetEnumerator() methods (there should be two of them!).

Here's how to implement IEnumerable<IUnit> :

 class TemperatureDimension : IEnumerable<IUnit> { private List<IUnit> _Units; public List<IUnit> Units { get { return _Units; } } public TemperatureDimension() { _Units = new List<IUnit> { new Celsius(), new Kelvin(), new Fahrenheit() }; } public IEnumerator<IUnit> GetEnumerator() { return _Units.GetEnumerator(); } System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { return GetEnumerator(); } } 

Does anyone know how to attach an archive?


Here is the archive in text format :-) http://pastebin.com/xUwaKxAT

Run the program (do not forget to change the path in Main to the one that suits you!), Get the archive, unpack it, get the Solution, in which both methods work with combo boxes. Here it is.

  • Sorry to have troubled you in vain. You're right: the binding works when implementing the IEnumerable <IUnit> interface, and when explicitly specifying a property in Xaml. I, as it turned out, had a different error: due to insufficiently debugged serialization, an instance of the TemperatureDimension class is created when creating an object, but after serialization it is reset to Null. Therefore, now the problem is solved. - maestro
  • @Alouette: the main thing is that the problem is solved! Happy to help. Even if it is not needed, perhaps the answer pushed you to search in the right place. - VladD