I'm having issues binding a WPF ComboBox in XAML.
Here is my object definition and collection:
public class AccountManager
{
public long UserCode { get; set; }
public string UserName { get; set; }
}
public partial class MainWindow : Window
{
public List<AccountManager> AccountManagers;
}
Here is the XAML definition of my ComboBox:
ComboBox Name="cbTestAccountManagers"
ItemsSource="{Binding AccountManagers}"
DisplayMemberPath="UserName"
SelectedValuePath="UserCode"
Width="250"
I'm not quite sure what I'm doing wrong here. I don't get any errors at run/load time. The ComboBox displays without any contents in the drop down. (It's empty).
Can someone point me in the right direction?
Thanks
Your problem is simple. Change
public List<AccountManager> AccountManagers;
to this
public List<AccountManager> AccountManagers { get; set; }
and make sure that you have these in your MainWindow constructor
public MainWindow()
{
InitializeComponent();
//Setup Account managers here
DataContext = this;
}
you can only bind to properties not fields and you need to ensure the proper data context
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With