c# - 将 IEnumerable<class> 的结果绑定(bind)到组合框

标签 c# wpf xaml data-binding collections

我正在尝试将 ViewModel 中的集合的结果绑定(bind)到组合框。下面是我的代码。任何帮助,将不胜感激。如果您需要查看其他内容或需要更多信息,请告诉我。

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 来设置这些值,我喜欢称之为“魔法”。我没有写此电子邮件部分。我只是想处理组合框中的元素。再次感谢!

最佳答案

如果您在任何地方都没有真正的 StaticResource,则不应使用它。对于您的情况,您最有可能想要执行以下操作:

GetEmailUsers() 方法更改为属性(只能绑定(bind)属性,不能绑定(bind)方法返回值):

public interface IEmailAddressesProvider
{
    string ProviderName { get; }
    IEnumerable<EmailAddress> EmailUsers { get; set;};
}

然后将您的 XAML 更改为:

<ComboBox x:Name="cboProviders" ItemsSource="{Binding AddressProviders.EmailUsers}" Grid.Row="0" Grid.Column="1"></ComboBox>

还要确保将页面的 DataContext 设置为 ViewModel 实例。

编辑:好的,刚刚了解到您错误地设置了DataContext。根据您所写的内容,您似乎将其设置在 XAML 中的某个位置,如下所示:

DataContext="clr-namespace:Alliance.Library.Email.EmailViewModel"

这本质上意味着您将数据上下文设置为字符串,然后尝试绑定(bind)到该字符串的属性,而该属性肯定不存在。我建议您检查运行时输出是否存在绑定(bind)错误,以确保情况确实如此。

如果确实如此,那么您需要正确设置DataContext。最简单的选择是在 View 的构造函数中执行此操作:

public MyView()
{
    DataContext = new Alliance.Library.Email.EmailViewModel();
}

关于c# - 将 IEnumerable<class> 的结果绑定(bind)到组合框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14775099/

相关文章:

c# - 如何在ajax回发后从代码隐藏调用javascript函数

c# - 仅对数据表中的选定行求和?

wpf - 如何更改 DrawingGroup.Children 标记中的笔的画笔颜色

wpf - 如何在我的 UserControl 中接受剪贴板中的粘贴?

c# - 区分x :Name and Name in Wpf application

c# - 将mysql内容插入html?

c# - 在 VisualStateManager 中交换画笔

wpf - 调整 XAML Grid.Background 图像的大小

wpf - 在 XAML : Stop an Event from Taking Place

c# - 在 C++ 和 C# 中写入二进制文件