在连接两个类数据用于视图 [英] Connect data in two classes for use in a View

查看:117
本文介绍了在连接两个类数据用于视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想一个IEnumberable收集绑定到一个组合框。我知道问题出在哪里,我实例化ViewModel的我......我的观点的DataContext的是一个完全不同的实例,并没有看到正在被set.I需要知道我需要怎么传过来的数据到数据正确的实例。我是新来WPF所以我可能不是解释一些这完全正确。下面是code,我觉得是相关的。如果你想别人看到什么,只是让我知道,我会尽我所能提供的信息。任何帮助将是AP preciated!我也总是会接受的技巧和窍门!

I am trying to bind an IEnumberable collection to a combobox. I know that the problem is where I am instantiating my ViewModel... The datacontext of my view is a totally different instance and is not seeing the data that is being set.I need to know how I need to pass over the data to the right instance. I am new to WPF so I may not being explaining some of this completely right. Below is the code that I find to be relevant. If you want to see anything else, just let me know and I will try my best to provide the information. Any help would be appreciated! I also will always accept tips and tricks!

code:

XAML:

 <telerik:RadComboBox x:Name="cboProviders" ItemsSource="{Binding Source=AddressProviders}" DisplayMemberPath="ProviderName" SelectedItem="{Binding SelectedAddressProvider, Mode=TwoWay}" Grid.Row="0" Grid.Column="1"/>

上面是我目前使用绑定。它知道有一个集合有,但它并不显示正被设置的数据

Above is the binding that I am currently using. It knows that there is a collection there but it does not show the data that is being set.

的DataContext:

DataContext:

public EmailAddressWindow()
        {
            InitializeComponent();
            this.DataContext = new EmailViewModel();

        }

此被设定为EmailViewModel

This is set to the EmailViewModel

EmailViewModel:

EmailViewModel:

   public IEnumerable<IEmailAddressesProvider> AddressProviders 
    {
        get
        {
            return _AddressProviders;
        }
        set
        {
            _AddressProviders = value;
            OnPropertyChanged("AddressProviders");
        }
    }
private string _SelectedAddressProvider;
public string SelectedAddressProvider
{
    get
    {
        return _SelectedAddressProvider;
    }
    set
    {
        _SelectedAddressProvider = value;
        OnPropertyChanged("SelectedAddressProvider");
    }
}

接口(IEmailAddressesProvider):

Interface (IEmailAddressesProvider):

    public interface IEmailAddressesProvider
    {
        string ProviderName { get; }
        IEnumerable<EmailAddress> GetEmailUsers();
    }
}

凡的ProviderName设置和定义方法EmailUsers(EmailAddressesProvider):

Where the ProviderName is set and the EmailUsers method is defined (EmailAddressesProvider):

[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;

        }
    }

在哪里被设置为视图模型(EmailService.cs)

Where is is being set to the ViewModel (EmailService.cs)

[ImportMany]
        public IEnumerable<IEmailAddressesProvider> AddressProviders { get; set; }

 EmailView ev = AllianceApp.Container.GetExportedValue<EmailView>();


            ev.ViewModel.AddressProviders = this.AddressProviders;

正如你所看到的,所使用的观点是EmailView ......不过,我需要使用我的窗口的datacontext其中,组合框定义的视图。我需要以某种方式连接类共享的信息。如果是这样,如何​​在世界上做我做。这甚至可能不是问题,但它是对我有意义的嘛。希望你们中的一个可以计算出来之前,我把我的电脑..同样,任何帮助是AP preciated。

As you can see here, the view being used is EmailView... However I need to be using the View defined in the datacontext of my window where the combobox is. Do I need to somehow connect the classes to share the information. If so, how in the world do I do that. This might not even be the problem, but it is the only thing that made sense to me. Hopefully one of you can figure it out before I throw my computer.. Again, any help is appreciated.

推荐答案

考虑使用的ObservableCollection 绑定,如果你已经有了集合为的IEnumerable 您可以轻松地将其转换,

Consider using ObservableCollection for binding, if you already have the collection as an IEnumerable you can easily convert it,

ObservableCollection<T> obsColl = new ObservableCollection<T>(originalIEnumerable)

的ObservableCollection 有一个构造函数的的IEnumerable 作为参数,使得它的一个浅拷贝

ObservableCollection has a constructor that takes an IEnumerable as a parameter and makes a shallow copy of it

这篇关于在连接两个类数据用于视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆