I am trying to bind the results from a collection in my ViewModel to a combobox. Below is my code. Any help would be appreciated. If you need to see something else or need more information let me know.
XAML:
DataContext="clr-namespace:Alliance.Library.Email.EmailViewModel"
<ComboBox x:Name="cboProviders" ItemsSource="{Binding Source=AddressProviders}" DisplayMemberPath="ProviderName" Grid.Row="0" Grid.Column="1"></ComboBox>
That is my combobox. I realize that the code for that is completely wrong, but I am new so I was trying to approach it with trial and error.
Code:
This is in my VeiwModel “EmailViewModel.cs”:
public IEnumerable<IEmailAddressesProvider> AddressProviders { get; set; }
This is my interface “IEmailAddressesProvider”:
public interface IEmailAddressesProvider
{
string ProviderName { get; }
IEnumerable<EmailAddress> GetEmailUsers();
}
}
Code for “EmailAddressProviders.cs” that contains GetEmailUsers():
[Export(typeof(IEmailAddressesProvider))]
public class EmailAddressProvider : IEmailAddressesProvider
{
#region Private Properties
private static readonly IEncryptionService encryptionService = AllianceApp.Container.GetExportedValue<IEncryptionService>();
#endregion
public string ProviderName
{
get { return "Alliance Users"; }
}
public IEnumerable<EmailAddress> GetEmailUsers()
{
IUserRepository userRepo = AllianceApp.Container.GetExportedValue<IUserRepository>();
IEnumerable<User> users = userRepo.GetAllUsers().Where(a => a.IsDeleted == false).OrderBy(a => a.UserID).AsEnumerable();
List<EmailAddress> AddressList = new List<EmailAddress>();
foreach (var user in users)
{
if (user.DisplayName != null && user.EmailAddress != null && user.DisplayName != string.Empty && user.EmailAddress != string.Empty)
AddressList.Add(new EmailAddress() { DisplayName = encryptionService.DecryptString(user.DisplayName), Email = encryptionService.DecryptString(user.EmailAddress) });
}
AddressList.OrderBy(u => u.DisplayName);
return AddressList;
}
}
I am using MEF so as to how these values are being set, I like to call ‘magic.’ I didn’t write the email portion of this. I am just trying to take care of getting the elements in the combobox. Thanks again!
If you don’t really have a StaticResource anywhere, you should not use it. For your situation, you most probably want to do the following:
Change
GetEmailUsers()method to a property (you can only bind properties, not methods return values):Then change your XAML to this:
Also make sure you set DataContext for your Page to your ViewModel instance.
EDIT: Okay, just understood that you’re setting
DataContextwrongly. Based on what you’ve written, you seem to set it somewhere in your XAML like this:This essentially means you’re setting your data context to a string, and then try to bind to that string’s properties, which certainly don’t exist. I’d recommend you to check your runtime output for a binding errors to make sure this is the case.
If that’s really so, then you need to set
DataContextcorrectly. The easiest option to begin with is to do this in your view’s constructor: