1

I'm trying to learn WPF Databinding with Entity Framework. I have implemented the tutorial in the link

and it works perfectly fine. I'm trying to insert a combo box myself and I want to bind it to category's name. But I couldn't achieve it. Here is my attempt : on XAML file :

<ComboBox HorizontalAlignment="Left" Margin="394,421,0,0" VerticalAlignment="Top" Width="120" Name="ComboCategory" Binding="{Binding Name}" />

and code-behind :

ComboCategory.ItemSource = _context.Categories.Local.ToList();

Can you tell me what I'm missing? Thanks.

jason
  • 6,962
  • 36
  • 117
  • 198

4 Answers4

1

You are missing DisplayMemberpath property here

<ComboBox HorizontalAlignment="Left" Margin="394,421,0,0" VerticalAlignment="Top" Width="120" Name="ComboCategory"  DisplayMemberPath = "Name" />
Sajeetharan
  • 216,225
  • 63
  • 350
  • 396
  • Is code-behind true? Because I'm getting an error on that line. – jason Jul 04 '14 at 07:25
  • I'm getting an error here : ComboCategory.ItemSource = _context.Categories.Local.ToList(); which is 'System.Windows.Controls.ComboBox' does not contain a definition for 'ItemSource' and no extension method 'ItemSource' accepting a first argument of type 'System.Windows.Controls.ComboBox' could be found (are you missing a using directive or an assembly reference?) – jason Jul 04 '14 at 07:27
  • is it wpf or windform? – Sajeetharan Jul 04 '14 at 07:28
1

Altough the use of the ItemSource is perfectly valid. I suggest you work with Data Binding. Here's a nice definition from MSDN:

Data binding is the process that establishes a connection between the application UI and business logic. If the binding has the correct settings and the data provides the proper notifications, then, when the data changes its value, the elements that are bound to the data reflect changes automatically. Data binding can also mean that if an outer representation of the data in an element changes, then the underlying data can be automatically updated to reflect the change. For example, if the user edits the value in a TextBox element, the underlying data value is automatically updated to reflect that change.

I have answered a question where someone also had a problem with binding items to a ListBox. This is not a ComboBox but the principle is the same. Click here to go the question, and here to go straight to the answer.

Basically it comes down to this:

  • Set up your UI
  • Bind your data

In following code, I changed the properties a bit according to the properties used in the tutorial.

XAML:

<ListBox Margin="20" ItemsSource="{Binding Products}"> 
    <ListBox.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="{Binding Path=Name}" />
                <TextBlock Text="{Binding Path=ProductId}" />
            </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

C#

public class Product 
{ 
    public int ProductId { get; set; } 
    public string Name { get; set; } 
    public int CategoryId { get; set; } 
    public virtual Category Category { get; set; } 
} 

public class ProductViewModel
{
    public List<Product> Products
    {
        get
        {
            return new List<Product>
            {
                new Product{ ProductId = 1, Name = "Product_1" },
                new Product{ ProductId = 2, Name = "Product_2" }
            };
        }
    }
}

//Following code can be placed in the Loaded event of the page:
DataContext = new ProductViewModel();
Community
  • 1
  • 1
Abbas
  • 14,186
  • 6
  • 41
  • 72
0

It worked when I used this in XAML:

<ComboBox HorizontalAlignment="Left" Margin="394,421,0,0" VerticalAlignment="Top" Width="120" Name="ComboCategory" DisplayMemberPath = "Name" ItemsSource="{Binding}" />
jason
  • 6,962
  • 36
  • 117
  • 198
0

Couldnt believe it, check the link

The issue is ItemsSource(plural) not ItemSource(singular)

Krik
  • 355
  • 4
  • 9