简体   繁体   English

将RIA服务结果和列表绑定到Silverlight4 MVVM中的ObserverableCollection

[英]Binding RIA service results and a list to an ObserverableCollection in Silverlight4 MVVM

I have an Entity result of List<string> GetTagsData that I need to somehow bind with checkboxes in an ObservableCollection and then to a DataGrid. 我有List<string> GetTagsData的Entity结果,我需要以某种方式将其与ObservableCollection中的复选框绑定,然后绑定到DataGrid。 These checkbox bindings correspond to a dynamic comma delimited string that is a subset of that entity result GetTagsData except it's in a comma delimited string. 这些复选框绑定对应于一个动态逗号分隔的字符串,该字符串是该实体结果GetTagsData的子集,但它位于逗号分隔的字符串中。 The user then can uncheck or check each item in the DataGrid and then we call an event handler to basically regenerate the comma delimited string. 然后,用户可以取消选中或选中DataGrid中的每个项目,然后调用事件处理程序以基本上重新生成逗号分隔的字符串。 My question is this, Is there a better way of architecting this solution? 我的问题是,是否有更好的架构此解决方案的方法? I see this as potential problematic to maintain in the future. 我认为这是将来可能需要维护的问题。 the only thing that I can't change is the comma delimited string coming in and out. 我唯一不能更改的是传入和传出的逗号分隔字符串。 and I have to build it in silverlight. 我必须在Silverlight中构建它。 Thanks ahead! 谢谢你!

Comma Delimited String 逗号分隔字符串

'Chicago','New York','Boston','Los Angeles'

Entity Result for List<string> GetTagsData List<string> GetTagsData的实体结果

GetTagsData = SecurityDomainContext.Current.vwBusinessUnits.Select(d => d.Market).Distinct().ToList();

ObservableCollection Class ObservableCollection类

    public class TagsCollection : ViewModelBase 
{
    private string _tag;   
    private bool _isSelected; 
    public string Tag
    {
        get
        { return _tag; }
        set 
        { _tag = value; }
    }

    public bool IsSelected
    {
        get 
        { return _isSelected; }
        set 
        { _isSelected = value; }
    }  

Now in my ViewModel I am able to iterate the Entity results into the Collection and bind that Collection to the DataGrid. 现在,在ViewModel中,我可以将Entity结果迭代到Collection中并将该Collection绑定到DataGrid。

private ObservableCollection<TagsCollection> GetTagsCollection(string colName)
        {
            ObservableCollection<TagsCollection> ocTags = new ObservableCollection<TagsCollection>();

                            foreach (string tag in GetTagsData)
                        {
                            if (DelimitedTagSet.Contains(tag.Trim()))
                            {
                                ocTags.Add(new TagsCollection { Tag = tag, IsSelected = true });
                            }
                            else
                            {
                                ocTags.Add(new TagsCollection { Tag = tag, IsSelected = false });
                            }                               
                        }
            return ocTags;
        }

Here's what the xaml view looks like 这是xaml视图的样子 在此处输入图片说明

Based on my understanding, the states you have to provide is a dynamic immutable list of options where you want to prompt user to enable/disable each of them, then return result as original string. 根据我的理解,您必须提供的状态是选项的动态不可变列表,您要在其中提示用户启用/禁用每个选项,然后将结果作为原始字符串返回。

If the list is a static list that you expect, for instance states, I'd surely suggest you having a list of states, and mapping against it. 如果该列表是您期望的静态列表,例如状态,那么我肯定会建议您有一个状态列表,并对其进行映射。

Make an Tag class that implement INotifyPropertyChanged, that has two props (name, selected) and remember to raise the PropertyChanged for each property change (I see it's excluded from your code, I assume for brevity purposes). 制作一个实现INotifyPropertyChanged的Tag类,该类具有两个属性(选择名称),并记住为每个属性更改引发PropertyChanged (为简洁起见,我认为它已从您的代码中排除)。

Make a List subclass, add a string constructor and override the ToString(), make all the parsing in this class and return the result on the ToString, then call it from you ViewModel. 创建一个List子类,添加一个字符串构造函数并覆盖ToString(),在该类中进行所有解析,然后在ToString上返回结果,然后从ViewModel对其进行调用。

I now realize that you tagged the question with 我现在意识到您用这个标记了问题 放射免疫 , so why would you want to bind against that comma delimited string? ,那么为什么要绑定该逗号分隔的字符串?

You might want to expose the options in the server and treat it like this, so the client doesn't know it's a comma delimited. 您可能希望在服务器中公开这些选项,并像这样对待它,因此客户端不知道这是逗号分隔的。

public class OptionsViewModel : ViewModelBase
{

  public OptionsViewModel()
  {
     IsBusy = True;
     var context = new MyDomainContext();
     _Tags = context.Tags;
     //if called elsewhere but from ctor, make sure context.IsLoading is false;
     //The Load method is throwing an exception if re-loading when a load is on.
     //Debug.Assert(!context.IsLoading);
     context.Load(
        context.GetTagsQuery(),
        (op) =>
        {
          if(op.HasError && !op.IsErrorHandled) op.MarkErrorAsHandlere();
          IsBusy = false;
        },
        null);
  }

  private readonly Ienumerable<Tag> _Tags;
  public Ienumerable<Tag> Tags
  {
    get
    {
      return _Tags;
    }
  }

}

In the server expose an operation that returns that datacontract (of Tag) and the server should do the comma string parsing. 在服务器中公开一个返回该数据合同(Tag的)的操作,服务器应进行逗号字符串解析。

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

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