简体   繁体   中英

WPF Databinding with a ComboBox

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.

您在这里缺少DisplayMemberpath属性

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

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();

当我在XAML中使用它时,它起作用了:

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

Couldnt believe it, check the link

The issue is Item s Source(plural) not ItemSource(singular)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM