Why isn't my combobox filling, when using EF?
-
09 April 2012 14:27
I'm starting a new WPF project, to learned Entity Framework and how it works with WPF. (I'm using VS 2010 with .NET 4.0, and EF 4.2.) I'm writing a page based WPF app. This is really early stuff, so there's very little on the page. I removed the grid on the page, and replaced it with a stackpanel. Here is the XAML:
<StackPanel> <TextBlock Margin="5,10,0,0" FontWeight="Bold" FontSize="20">SEARCH MEDIA LIBRARY</TextBlock> <StackPanel Orientation="Horizontal" Margin="5,10,0,0" Name="SelectShowStackPanel"> <TextBlock FontSize="15" VerticalAlignment="Center">Category to Search:</TextBlock> <ComboBox Height="23" Name="comboShows" Margin="5,0" FontSize="15" SelectionChanged="comboShows_SelectionChanged" DisplayMemberPath="{Binding Path=ShowName}" SelectedValuePath="{Binding Path=ShowCode}" /> <Image x:Name="ImageOfTheShow" Stretch="None" Source="/Images/no%20image.gif" /> </StackPanel> </StackPanel>For this app I'm not using MVVM; it's going to be code-behind. I'm focusing on how WPF and EF work together, and at this point I'm not a MVVM guru, so I don't want to take on too much as it is. Here's the code from my page load event:
private void Page_Loaded(object sender, RoutedEventArgs e) { //using (var context = new MediaData.VideoEntities()) //{ // var typeOfShows = context.TypeOfShows // .OrderBy(t => t.ShowName); // SelectShowStackPanel.DataContext = typeOfShows; // //comboShows.ItemsSource = typeOfShows; // //comboShows.DisplayMemberPath = "ShowName"; //} var context = new MediaData.VideoEntities(); var typeOfShows = context.TypeOfShows .OrderBy(t => t.ShowName); SelectShowStackPanel.DataContext = typeOfShows; }
As you can see, I initially had a using statement in which I loaded the ItemsSouce and DisplayMemberPath properties in the page load event. Then I thought, no that doesne't make sense, this is WPF, not VB6. So I named the stackpanel surrounding the combobox and assigned the datacontext property of that stackpanel with the data from my database, using EF, but it doesn't populate the combobox. It did when I did it the old fashioned way, but I don't want to do it that way anymore. So, what have I done wrong?Rod
Semua Balasan
-
09 April 2012 14:58
I think you have to bind the ItemsSource too. You're not binding it to anything. Try this on XAML:
ItemsSource="{Binding}"
-
11 April 2012 3:17
Thank you, Joba, for replying. I tried what you suggested, but it didn't seem to work. Here's the XAML for the stackpanel now:
<StackPanel> <TextBlock Margin="5,10,0,0" FontWeight="Bold" FontSize="20">SEARCH MEDIA LIBRARY</TextBlock> <StackPanel Orientation="Horizontal" Margin="5,10,0,0" Name="SelectShowStackPanel"> <TextBlock FontSize="15" VerticalAlignment="Center">Category to Search:</TextBlock> <ComboBox Height="23" Name="comboShows" Margin="5,0" FontSize="15" SelectionChanged="comboShows_SelectionChanged" DisplayMemberPath="{Binding Path=ShowName}" SelectedValuePath="{Binding Path=ShowCode}" ItemsSource="{Binding}" Width="200" /> <Image x:Name="ImageOfTheShow" Stretch="None" Source="/Images/no%20image.gif" /> </StackPanel> </StackPanel>Rod
-
11 April 2012 4:07
which need convert binding data.
try to use data-template to format the data entity or binding the date using customize entity
- Diedit oleh Duan JunyiMVP 11 April 2012 4:07
-
11 April 2012 11:34
Well, my last guess is this:
SelectShowStackPanel.DataContext = typeOfShows.ToList();
If didn't work, Duan maybe is right.
-
11 April 2012 12:10
DisplayMemberPath is just the name/path of the property as a string,
not a binding.DisplayMemberPath="ShowName" ItemsSource="{Binding}"
should help, if TypeOfShow has prop named "ShowName".
- Ditandai sebagai Jawaban oleh Doctor-Who 14 April 2012 16:16
-
14 April 2012 16:16Man, I hate making simple mistakes. Thank you, Christoph.
Rod