wpf 将 IEnumerable<class> 的结果绑定到组合框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14775099/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Binding the results of an IEnumerable<class> to a combobox
提问by JLott
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.
我正在尝试将 ViewModel 中集合的结果绑定到组合框。下面是我的代码。任何帮助,将不胜感激。如果您需要查看其他内容或需要更多信息,请告诉我。
XAML:
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":
这是在我的 VeiwModel "EmailViewModel.cs" 中:
public IEnumerable<IEmailAddressesProvider> AddressProviders { get; set; }
This is my interface "IEmailAddressesProvider":
这是我的界面“IEmailAddressesProvider”:
public interface IEmailAddressesProvider
{
string ProviderName { get; }
IEnumerable<EmailAddress> GetEmailUsers();
}
}
Code for "EmailAddressProviders.cs" that contains 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;
}
}
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!
我正在使用 MEF 以了解如何设置这些值,我喜欢称之为“魔术”。我没有写这个的电子邮件部分。我只是想照顾获取组合框中的元素。再次感谢!
采纳答案by Haspemulator
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:
如果您在任何地方都没有真正的 StaticResource,则不应使用它。对于您的情况,您很可能希望执行以下操作:
Change GetEmailUsers()method to a property (you can only bind properties, not methods return values):
将GetEmailUsers()方法更改为属性(只能绑定属性,不能绑定方法返回值):
public interface IEmailAddressesProvider
{
string ProviderName { get; }
IEnumerable<EmailAddress> EmailUsers { get; set;};
}
Then change your XAML to this:
然后将您的 XAML 更改为:
<ComboBox x:Name="cboProviders" ItemsSource="{Binding AddressProviders.EmailUsers}" Grid.Row="0" Grid.Column="1"></ComboBox>
Also make sure you set DataContext for your Page to your ViewModel instance.
还要确保您将 Page 的 DataContext 设置为您的 ViewModel 实例。
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:
编辑:好的,只是明白你设置DataContext错误。根据您所写的内容,您似乎将它设置在 XAML 中的某个位置,如下所示:
DataContext="clr-namespace:Alliance.Library.Email.EmailViewModel"
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:
如果确实如此,那么您需要DataContext正确设置。最简单的选择是在视图的构造函数中执行此操作:
public MyView()
{
DataContext = new Alliance.Library.Email.EmailViewModel();
}
回答by Cameron MacFarland
I think this is what you want (in the View).
我认为这就是你想要的(在视图中)。
<ComboBox
x:Name="cboProviders"
ItemsSource="{Binding Source=AddressProviders}"
DisplayMemberPath="ProviderName"
Grid.Row="0" Grid.Column="1"></ComboBox>
DisplayMemberPathon MSDN.
MSDN 上的DisplayMemberPath。

