0

我正在尝试从另一个具有对象列表的对象创建对象。但是我在进行铸造时遇到铸造错误。有没有办法将不同集合的对象投射到另一个集合上。

代码:

public List<TreeViewModel> getAllTreeNodesFromModel()
{
    treeNodeViewModel = treeModel.getTreeNodes().Select(a => new TreeViewModel
      {
        Children = a.Children.Select(c => new TreeViewModel{Value = c.Value}).ToList(),
        Value = a.Value,
       }).ToList();

      return treeNodeViewModel;
  }

// Making list observable
public ObservableCollection<TreeViewModel> TreeView
{
    get
    {
        return treeView; 
    }

    set
    {
        if (treeView == value) return;
        treeView = value;
        NotifyPropertyChanged("TreeView");
    }
}

XAML:

<TreeView Margin="644,137,6,6" Grid.RowSpan="2" ItemsSource="{Binding TreeView}">
   <TreeView.Resources>
       <HierarchicalDataTemplate DataType="{x:Type local:MainWindowModel}" ItemsSource="{Binding Children}">
           <CheckBox IsChecked="{Binding Value}" />
        </HierarchicalDataTemplate>
    </TreeView.Resources>
</TreeView>

这是我的模型:

public class TreeModel
{
    public string Name { get; set; }
    public List<TreeModel> Children { get; set; }
    public string Value { get; set; }

    public TreeModel()
    {
        Children = new List<TreeModel>();
    }

    public TreeModel(string name, List<TreeModel> children)
    {

        this.Name = name;
        this.Children = children;
    }

    public List<TreeModel> BuildTree(IEnumerable<string> strings)
    {
        return (
          from s in strings
          let split = s.Split('.')
          group s by s.Split('.')[0] into g  // Group by first component (before /)
          select new TreeModel
          {
              Value = g.Key,
              Children = BuildTree(            // Recursively build children
                from s in g
                where s.Length > g.Key.Length + 1
                select s.Substring(g.Key.Length + 1)) // Select remaining components
          }
          ).ToList();
    }

    // View Model
    public string Value { get; set; }
    public  List<TreeViewModel> Children { get; set; }

    // List VM mapped from Model
    public List<TreeViewModel> getAllTreeNodesFromModel()
    {
         treeNodeViewModel = treeModel.getTreeNodes().Select(a => new TreeViewModel
         {
             Children = a.Children.Cast<TreeViewModel>().ToList(),
             Value = a.Value,

         }).ToList();

         return treeNodeViewModel;
     }

XAML:

<TreeView Margin="644,137,6,6" Grid.RowSpan="2" ItemsSource="{Binding TreeView}"  >
    <TreeView.Resources>
        <HierarchicalDataTemplate DataType="{x:Type local:MainWindowModel}"  ItemsSource="{Binding Children}">
            <CheckBox IsChecked="{Binding Value}" />
        </HierarchicalDataTemplate>
    </TreeView.Resources>
</TreeView>
4

1 回答 1

1

从评论中复制答案:您应该为每个模型的孩子创建 TreeViewModel,而不是强制转换:

Children = a.Children.Select(c => new TreeViewModel{Value = c.Value}).ToList()

作为Children递归一样,最好这样写:

 public List<TreeViewModel> getAllTreeNodesFromModel()
 {
    treeNodeViewModel = treeModel.getTreeNodes().Select(a => getChildTreeNodesFromModel(a)).ToList();

    return treeNodeViewModel;
  }

 public List<TreeViewModel> getChildTreeNodesFromModel(TreeModel a_model)
 {
      return new TreeViewModel
      {
           Value = a_model.Value,
           Children = a_model.Children.Select(c => getChildTreeNodesFromModel(c)).ToList()
      }
 }
于 2013-05-31T14:16:21.673 回答