I am trying to understand DataForm as implemented in the November 2009 toolbox, and I cannot figure out how to associate a ComboBox with an enumeration. Does anyone know how DataForm does this automatically?
Background
First, I created a class and Enum, following this article, and allowed the DataForm to generate fields. The DataForm generated a TextBox field for the Name field and (what I assume) for a ComboBox for the enum genres field.
My first goal in understanding how to set up a DataForm is to reproduce what is done in auto-generation. I managed to make TextBoxes (and a DatePicker excluded from this code), but I'm struggling to associate a ComboBox with an enumeration.
Here are the classes (simplified):
public class Movie { public string Name { get; set; } public Genres Genre { get; set; } } public enum Genres { Comedy, Fantasy, Drama, Thriller }
and then in MainPage I do this:
private ObservableCollection<Movie> movies = new ObservableCollection<Movie>(); private void UserControl_Loaded(object sender, RoutedEventArgs e) { Movie movie = new Movie() { Name = "Fred", Genre = Genres.Thriller }; movies.Add(movie); myDataForm.ItemsSource = movies; }
and in MainPage.xaml in the grid:
<dataFormToolkit:DataForm x:Name="myDataForm" AutoEdit="False" AutoCommit="False" Header="Foo Movie DB"> </dataFormToolkit:DataForm>
for automatically generated material. When trying to generate it manually, I got:
<dataFormToolkit:DataForm x:Name="myDataForm" AutoEdit="False" AutoCommit="False" Header="Foo Movie DB"> <StackPanel Orientation="Vertical"> <dataFormToolkit:DataField> <TextBox Text="{Binding Name, Mode=TwoWay}"/> </dataFormToolkit:DataField> <dataFormToolkit:DataField> <ComboBox ItemsSource="{Binding Genres}" SelectedItem="{Binding Genre, Mode=TwoWay}" /> </dataFormToolkit:DataField> </StackPanel> </dataFormToolkit:DataForm>
but ComboBox is not working. There are many articles covering this, but it seems that much of what they offer is too much for an automatic generator (for example, for a subclass of ComboBox to provide SelectedValue). Do you know how tools do this for us?