Reputation: 1372
I'm just getting started with WPF and Visual Studio 2010. When I try the following simple ComboBox binding, I get error messages.
XAML:
...
<ComboBox Height="23" HorizontalAlignment="Left"
Margin="33,18,0,0" Name="comboBox1" VerticalAlignment="Top" Width="120"
ItemsSource="{Binding Path=test}"/>
...
Code File:
public partial class SetupWindow2 : Window
{
public List<string> test { get; set; }
public SetupWindow2()
{
test = new List<string>() { "1", "2", "3" };
InitializeComponent();
}
}
Error Messages:
System.Windows.Data Information: 10 : Cannot retrieve value using the binding and no valid fallback value exists; using default instead. BindingExpression:Path=test; DataItem=null; target element is 'ComboBox' (Name='comboBox1'); target property is 'ItemsSource' (type 'IEnumerable')
System.Windows.Data Information: 40 : BindingExpression path error: 'test' property not found for 'object' because data item is null. This could happen because the data provider has not produced any data yet. BindingExpression:Path=test; DataItem=null; target element is 'ComboBox' (Name='comboBox1'); target property is 'ItemsSource' (type 'IEnumerable')
System.Windows.Data Information: 19 : BindingExpression cannot retrieve value due to missing information. BindingExpression:Path=test; DataItem=null; target element is 'ComboBox' (Name='comboBox1'); target property is 'ItemsSource' (type 'IEnumerable')
System.Windows.Data Information: 20 : BindingExpression cannot retrieve value from null data item. This could happen when binding is detached or when binding to a Nullable type that has no value. BindingExpression:Path=test; DataItem=null; target element is 'ComboBox' (Name='comboBox1'); target property is 'ItemsSource' (type 'IEnumerable')
System.Windows.Data Information: 10 : Cannot retrieve value using the binding and no valid fallback value exists; using default instead. BindingExpression:Path=test; DataItem=null; target element is 'ComboBox' (Name='comboBox1'); target property is 'ItemsSource' (type 'IEnumerable')
Where is the problem here?
Upvotes: 2
Views: 6777
Reputation: 22445
Whenever binding fails you should check the DataContext first and then the Binding expression.
In your case I would say the right datacontext is missing, so if your combobox is in your setupwindow2 you should add:
public partial class SetupWindow2 : Window
{
public List<string> test { get; set; }
public SetupWindow2()
{
test = new List<string>() { "1", "2", "3" };
this.Datacontext = this;
}
}
Upvotes: 4