无法在ComboBox WPF中使用Multiselect项进行保存

时间:2021-12-05 14:14:50

Im creating an application using Entity Framework and MVVM. Within my app, I have a combobox that I have created following using the following link

使用实体框架和MVVM创建应用程序。在我的应用中,我有一个combobox,我使用以下链接创建了它

Within this link, It is exactly what I want to achieve. Enable a user to select multiple items from a combobox. The image below will give you a better understanding of that I want to create.

在这个链接中,这正是我想要实现的。允许用户从一个组合框中选择多个项目。下面的图片会让你更好地理解我想要创建的。

无法在ComboBox WPF中使用Multiselect项进行保存

So far so good. But within my application, I assign my Properties to the dataobjects within my EntityFramework entities to save data into the database.

目前为止一切都很顺利。但在我的应用程序中,我将属性分配给EntityFramework实体中的dataobjects,以便将数据保存到数据库中。

VM constructor;

虚拟机的构造函数;

    public EmployeeViewModel(DataObjects.Employee e)
        : base("")
    {
        EmployeeID = e.EmployeeID;
        Title = e.Title;
        FirstName = e.FirstName;
        Surname = e.Surname;
        Position = e.Position;
     }

But like in the example web link, the type of which the combobox is bound to is as follows;

但是就像示例web链接一样,combobox绑定的类型如下;

private Dictionary<string, object> _items;
public Dictionary<string, object> Items
    {
        get
        {
             return _items;
        }
        set
        {
             _items = value;
             OnPropertyChanged("Items");
        }
    }

So, if i do something like the following within my constructor, it produces the following error;

因此,如果我在构造函数中执行如下操作,它会产生以下错误;

 Items = e.BenefitsProvided;

Error 1 Cannot implicitly convert type 'string' to 'System.Collections.Generic.Dictionary'

错误1不能隐式地将类型'string'转换为'System.Collections.Generic.Dictionary'

Which leads me to my question. How do I manage to assign the type Dictionary<string, object> to the type of which is in my Employee table called BenefitsProvided, which is a nvarchar? Is there a converter which could be used?

这就引出了我的问题。我如何分配类型字典 给我的Employee表中的类型,叫做benefit - provide,它是一个nvarchar?有可以使用的转换器吗? ,对象>

Any help or alternative methods to achieve this would be very appreciated.

如有任何帮助或替代方法,将不胜感激。

1 个解决方案

#1


0  

If I understand your model correctly the property BenefitsProvided is a string that contains the various benefits of the employee as strings in a comma-separated list, like "Voucher,Accomodation,Medical". In this case you can split the string into an array of substrings and create the dictionary with this array:

如果我正确地理解了您的模型,那么所提供的财产收益是一个字符串,其中包含了员工的各种收益,如“凭证、住宿、医疗”等。在这种情况下,您可以将字符串分割成一组子字符串,并用这个数组创建字典:

Items = new Dictionary<string, object>();
string[] elements = e.BenefitsProvided.Split(',');
foreach (var element in elements)
    Items.Add(element, element);

Or in a more compact form using LINQ:

或者使用LINQ的更紧凑的形式:

Items = e.BenefitsProvided.Split(',').ToDictionary(s => s, s => (object)s);

I was assuming here that both the string key and the object value of the dictionary are supposed to be the same text.

我假设字符串键和字典的对象值应该是相同的文本。

#1


0  

If I understand your model correctly the property BenefitsProvided is a string that contains the various benefits of the employee as strings in a comma-separated list, like "Voucher,Accomodation,Medical". In this case you can split the string into an array of substrings and create the dictionary with this array:

如果我正确地理解了您的模型,那么所提供的财产收益是一个字符串,其中包含了员工的各种收益,如“凭证、住宿、医疗”等。在这种情况下,您可以将字符串分割成一组子字符串,并用这个数组创建字典:

Items = new Dictionary<string, object>();
string[] elements = e.BenefitsProvided.Split(',');
foreach (var element in elements)
    Items.Add(element, element);

Or in a more compact form using LINQ:

或者使用LINQ的更紧凑的形式:

Items = e.BenefitsProvided.Split(',').ToDictionary(s => s, s => (object)s);

I was assuming here that both the string key and the object value of the dictionary are supposed to be the same text.

我假设字符串键和字典的对象值应该是相同的文本。