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!