我想將ViewModel中的集合的結果綁定到組合框。以下是我的代碼。任何幫助,將不勝感激。如果您需要查看其他信息或需要更多信息,請告訴我。將IEnumerable <class>的結果綁定到組合框
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();
}
}
守則 「EmailAddressProviders.cs」包含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;
}
}
我正在使用MEF以便如何設置這些值,我喜歡稱之爲'魔術'。我沒有寫這個電子郵件部分。我只是在考慮獲取組合框中的元素。再次感謝!
http://www.wintellect.com/cs/blogs/jlikness/archive/2010/06/10/tips-and-tricks-for-inotifypropertychanged.aspx – spender 2013-02-08 14:51:21
1。什麼不工作? 2.發佈如何定義您用於綁定的StaticResource。 – Haspemulator 2013-02-08 14:51:24
它只是不包含集合,我沒有定義我的StaticResource。就像我說的,我對此很新。我發現它並不那麼直觀。 – JLott 2013-02-08 14:52:28