Note that there are some explanatory texts on larger screens.

plurals
  1. POUnable to cast object of type 'Model.TreeModel' to type 'ViewModels.TreeViewModel'
    primarykey
    data
    text
    <p>I am trying to create objects from another object which has a list of objects. But I am getting a casting error on the line where I do the casting. Is there a way to cast object of different collection on to another collection.</p> <p>Code:</p> <pre><code>public List&lt;TreeViewModel&gt; getAllTreeNodesFromModel() { treeNodeViewModel = treeModel.getTreeNodes().Select(a =&gt; new TreeViewModel { Children = a.Children.Select(c =&gt; new TreeViewModel{Value = c.Value}).ToList(), Value = a.Value, }).ToList(); return treeNodeViewModel; } // Making list observable public ObservableCollection&lt;TreeViewModel&gt; TreeView { get { return treeView; } set { if (treeView == value) return; treeView = value; NotifyPropertyChanged("TreeView"); } } </code></pre> <p>XAML:</p> <pre><code>&lt;TreeView Margin="644,137,6,6" Grid.RowSpan="2" ItemsSource="{Binding TreeView}"&gt; &lt;TreeView.Resources&gt; &lt;HierarchicalDataTemplate DataType="{x:Type local:MainWindowModel}" ItemsSource="{Binding Children}"&gt; &lt;CheckBox IsChecked="{Binding Value}" /&gt; &lt;/HierarchicalDataTemplate&gt; &lt;/TreeView.Resources&gt; &lt;/TreeView&gt; </code></pre> <p>Here is my model:</p> <pre><code>public class TreeModel { public string Name { get; set; } public List&lt;TreeModel&gt; Children { get; set; } public string Value { get; set; } public TreeModel() { Children = new List&lt;TreeModel&gt;(); } public TreeModel(string name, List&lt;TreeModel&gt; children) { this.Name = name; this.Children = children; } public List&lt;TreeModel&gt; BuildTree(IEnumerable&lt;string&gt; 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 &gt; g.Key.Length + 1 select s.Substring(g.Key.Length + 1)) // Select remaining components } ).ToList(); } // View Model public string Value { get; set; } public List&lt;TreeViewModel&gt; Children { get; set; } // List VM mapped from Model public List&lt;TreeViewModel&gt; getAllTreeNodesFromModel() { treeNodeViewModel = treeModel.getTreeNodes().Select(a =&gt; new TreeViewModel { Children = a.Children.Cast&lt;TreeViewModel&gt;().ToList(), Value = a.Value, }).ToList(); return treeNodeViewModel; } </code></pre> <p>XAML:</p> <pre><code>&lt;TreeView Margin="644,137,6,6" Grid.RowSpan="2" ItemsSource="{Binding TreeView}" &gt; &lt;TreeView.Resources&gt; &lt;HierarchicalDataTemplate DataType="{x:Type local:MainWindowModel}" ItemsSource="{Binding Children}"&gt; &lt;CheckBox IsChecked="{Binding Value}" /&gt; &lt;/HierarchicalDataTemplate&gt; &lt;/TreeView.Resources&gt; &lt;/TreeView&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload