简体   繁体   English

如何将ComboBox绑定到列表 <string> 在WPF中

[英]How to bind a ComboBox to List<string> in WPF

I'm trying to bind a ComboBox to List<string> Units. 我正在尝试将ComboBox绑定到List<string>单元。 But it doesn't display anything. 但是它什么也没显示。 Here is the code I have put together so far. 到目前为止,这是我整理的代码。

<ComboBox ItemsSource="{Binding Path=Units}"/>

public partial class Statusbar
{

    public List<string> Units { get; set; }

    public Statusbar()
    {
        InitializeComponent();

        Units = new List<string> {"KN-mm", "KN-cm", "KN-m"};

    }
}

You have to specify the source object of the binding. 您必须指定绑定的源对象。 One way would be to set the control's DataContext property: 一种方法是设置控件的DataContext属性:

public Statusbar()
{
    InitializeComponent();

    Units = new List<string> {"KN-mm", "KN-cm", "KN-m"};
    DataContext = this;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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