简单的 WPF ComboBox ItemsSource 绑定不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20354239/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 10:09:12  来源:igfitidea点击:

Simple WPF ComboBox ItemsSource Binding doesn't work

.netwpfxamlcombobox

提问by LTR

I'm just getting started with WPF and Visual Studio 2010. When I try the following simple ComboBox binding, I get error messages.

我刚刚开始使用 WPF 和 Visual Studio 2010。当我尝试以下简单的 ComboBox 绑定时,我收到错误消息。

XAML:

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?

这里的问题在哪里?

回答by blindmeis

Whenever binding fails you should check the DataContext first and then the Binding expression.

每当绑定失败时,您应该先检查 DataContext,然后再检查 Binding 表达式。

In your case I would say the right datacontext is missing, so if your combobox is in your setupwindow2 you should add:

在你的情况下,我会说缺少正确的数据上下文,所以如果你的组合框在你的 setupwindow2 中,你应该添加:

public partial class SetupWindow2 : Window
{
   public List<string> test { get; set; }

   public SetupWindow2()
   {
      test = new List<string>() { "1", "2", "3" };
      this.Datacontext = this;
   }
}