I’m new to WPF and trying to figure out all this databinding stuff. When I do the following in my code, my ComboBox is populated when I run my application:
public NewForm()
{
InitializeComponent();
Product.ItemsSource = Products;
}
public List<string> Products
{
get { return _productsComponents.Keys.ToList(); }
}
However, in my XAML when I have the following, the ComboBox has no content in it when I run my application:
<ComboBox Height="23" HorizontalAlignment="Left" Margin="138,116,0,0"
Name="Product" VerticalAlignment="Top" Width="120"
ItemsSource="{Binding Path=Products}"/>
Am I referencing something incorrectly? This tutorial was helpful but he never set ItemsSource in XAML, always in C#.
By default, you’re actually binding not to the form itself, but to the object assigned to the
DataContextproperty. This facilitates using a view model to manage all the data outside the codebehind files.You can probably assign the form itself to the
DataContextproperty in the constructorYou can also bind to the form in any of several ways. Here is one:
I don’t think that
Productsneeds to be aDependencyPropertyhere, but don’t quote me on that, and as long as the collection is not subject to change, you don’t need to worry about update notifications.