简体   繁体   English

使用RIA服务在Silverlight中进行数据绑定

[英]Databinding in Silverlight with RIA Services

I'm trying to display the content of a table in a combobox. 我正在尝试在组合框中显示表格的内容。

I'm using the MVVM pattern and in my viewmodel class if I write this it works: 我正在使用MVVM模式,并且在我的viewmodel类中,如果我写它有效:

private IEnumerable<EventType> _eventTypes;

public ManageProfileModel()
        {
            _referenceData = new ReferenceDataContext();

            _referenceData.Load(_referenceData.GetEventTypesQuery(), false);

            _eventTypes = _referenceData.EventTypes;
        }

Like this the combobox is displaying the data. 像这样,组合框显示数据。

However, I want the _eventTypes to be a List: 但是,我希望_eventTypes是一个List:

private List<EventType> _eventTypes;

But if I write this: 但如果我写这个:

public ManageProfileModel()
        {
            _referenceData = new ReferenceDataContext();

            _referenceData.Load(_referenceData.GetEventTypesQuery(), false);

            _eventTypes = _referenceData.EventTypes.ToList();
        }

then the combobox is empty. 然后组合框是空的。 What is wrong with that? 这有什么问题?

I want to use a List, because I want to be able to add and remove data in the list. 我想使用List,因为我希望能够在列表中添加和删除数据。

Best regards. 最好的祝福。

If I remember correctly, you can not convert IEnumerable to IList directly. 如果我没记错的话,你不能直接将IEnumerable转换为IList。 It is little tricky. 这有点棘手。 I would use of the options from the following link. 我会使用以下链接中的选项。 I have it in bookmark since I ran into the same problem. 我在书签中有它,因为我遇到了同样的问题。 http://devlicio.us/blogs/derik_whittaker/archive/2008/03/28/simple-way-to-convert-ienumerable-lt-entity-gt-to-list-lt-ientity-gt.aspx http://devlicio.us/blogs/derik_whittaker/archive/2008/03/28/simple-way-to-convert-ienumerable-lt-entity-gt-to-list-lt-ientity-gt.aspx

or look at this link 或者看看这个链接

http://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/af225aa0-1cf4-40dd-ac3e-e7a19edaef00 http://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/af225aa0-1cf4-40dd-ac3e-e7a19edaef00

DomainContext.Load is asynchronous, so in your second example you're creating a list that's most likely empty because the EntitySet hasn't finished loading yet. DomainContext.Load是异步的,因此在您的第二个示例中,您创建的列表很可能为空,因为EntitySet尚未完成加载。 Use the code posted by StackOverflowException to defer creating the list until the EntitySet has been populated and it should work. 使用StackOverflowException发布的代码来推迟创建列表,直到填充了EntitySet并且它应该可以工作。

just a shot straight from the head... 只是直接射击头部......

did you try to add something like propertychanged event for your list? 你是否尝试为列表添加类似propertychanged事件的内容? so it could be that the data came async and the property was not informed about the change... 所以可能是数据异步,财产没有被告知变化......

like I said ... 就像我说的 ...

 private List<EventType> _eventTypes;
 public List<EventType> EventTypes
    {
        get { return _eventTypes; }
        set
        {
            _eventTypes = value;
            RaisePropertyChanged("EventTypes");
        }
    }

and take also a look at ObservableCollections... 并看看ObservableCollections ......

Like I said just a shot... 就像我说的那样......

Hope this helps 希望这可以帮助

I don't have much MVVM exposure but with silverlight + RIA, I usually do something like this. 我没有太多的MVVM曝光但是使用silverlight + RIA,我通常做这样的事情。

private List<EventType> _eventTypes;
public ManageProfileModel()
{
    _referenceData = new ReferenceDataContext();

    var op = _referenceData.Load(_referenceData.GetEventTypesQuery(), false);
    op.Completed += op_Completed;

}

void po_Completed(object sender, EventArgs e)
{
    var op = ( InvokeOperation<IEnumerable<EventType>>)sender;
    _eventTypes = op.Value.ToList();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM