简体   繁体   中英

How to bind a ComboBox to List<string> in WPF

I'm trying to bind a ComboBox to List<string> Units. 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:

public Statusbar()
{
    InitializeComponent();

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

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