我正在尝试将 ViewModel 中集合的结果绑定到组合框。下面是我的代码。任何帮助,将不胜感激。如果您需要查看其他内容或需要更多信息,请告诉我。
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>
那是我的组合框。我意识到该代码是完全错误的,但我是新手,所以我试图通过反复试验来解决它。
代码:
这是在我的 VeiwModel “EmailViewModel.cs”中:
public IEnumerable<IEmailAddressesProvider> AddressProviders { get; set; }
这是我的界面“IEmailAddressesProvider”:
public interface IEmailAddressesProvider
{
string ProviderName { get; }
IEnumerable<EmailAddress> GetEmailUsers();
}
}
包含 GetEmailUsers() 的“EmailAddressProviders.cs”代码:
[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;
}
}
我正在使用 MEF 来了解如何设置这些值,我喜欢称之为“魔术”。我没有写这个的电子邮件部分。我只是想照顾组合框中的元素。再次感谢!