Hey all! This is a simple MVVM treeview project which displays (is supposed to display!) a treeView of Tasks and a textbox showing the Task contents (TaskName for instance). The problem is that the tree items are not shown at all! These are the classes used:
public class TaskTreeViewModel
{
TaskViewModel vmRooT;
List<TaskViewModel> vmFirstGeneration;
public TaskTreeViewModel(Task rootTask)
{
vmRooT = new TaskViewModel(rootTask);
vmFirstGeneration = new List<TaskViewModel>(new TaskViewModel[] { vmRooT });
}
}
class TaskViewModel : INotifyPropertyChanged
{
private Task vmTask;
private List<TaskViewModel> vmSubTasks;
TaskViewModel vmParent;
bool vmIsExpanded;
bool vmIsSelected;
#region properties
public string Name
{
get
{return vmTask.TaskName;}
}
public List<TaskViewModel> SubTasks
{
get
{return vmSubTasks;}
}
public TaskViewModel Parent
{
get
{return vmParent;}
}
#endregion
#region ctors
private TaskViewModel(Task task, TaskViewModel parent)
{
vmTask = task;
vmParent = parent;
vmSubTasks = new List<TaskViewModel>(
(from t in vmTask.SubTasks
select new TaskViewModel(t, this)).ToList<TaskViewModel>());
}
public TaskViewModel(Task task)
: this(task, null)
{ }
#endregion
#region Presentation Members
public bool IsExpanded
{
get { return vmIsExpanded; }
set
{
if (value != vmIsExpanded)
{
vmIsExpanded = value;
this.OnPropertyChanged("IsExpanded");
}
if (vmIsExpanded && vmParent != null)
vmParent.vmIsExpanded = true;
}
}
public bool IsSelected
{
get { return vmIsSelected; }
set
{
if (value != vmIsSelected)
{
vmIsSelected = value;
this.OnPropertyChanged("IsSelected");
}
}
}
#endregion
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
if (this.PropertyChanged != null)
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
#endregion
}
public class Task
{
public string TaskName { get; set; }
public string Description { get; set; }
public List<Task> SubTasks { get; set; }
#region ctors
public Task()
{
TaskName = string.Empty;
Description = string.Empty;
SubTasks = new List<Task>();
}
public Task(string strTaskName)
: this()
{
TaskName = strTaskName;
}
public Task(string strTaskName, string strDescription)
: this(strTaskName)
{
Description = strDescription;
}
public Task(string strTaskName, List<Task> subs)
: this(strTaskName)
{
SubTasks = subs;
}
public Task(string strTaskName, string strDescription, List<Task> subs)
: this(strTaskName, strDescription)
{
SubTasks = subs;
}
public Task(string strTaskName, string strDescription, params string[] strSubTasksNames)
: this(strTaskName, strDescription)
{
SubTasks = new List<Task>();
foreach (var strSub in strSubTasksNames)
{
SubTasks.Add(new Task(strSub));
}
}
#endregion ctors
}
And these are the XAML file and its code-behind:
<Window x:Class="Task_it__with_MVVM_.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Task it!" Height="570" Width="850" WindowStartupLocation="CenterScreen">
<Grid FlowDirection="RightToLeft" Margin="10">
<Grid.ColumnDefinitions>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
<TreeView Grid.Column="0" Margin="10" MinHeight="280" ItemsSource="{Binding vmFirstGeneration}">
<TreeView.ItemContainerStyle>
<Style TargetType="{x:Type TreeViewItem}">
<Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}" />
<Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />
<Setter Property="FontWeight" Value="Normal" />
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="FontWeight" Value="Bold" />
</Trigger>
</Style.Triggers>
</Style>
</TreeView.ItemContainerStyle>
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding SubTasks}">
<TextBlock Text="{Binding Name}" FontSize="14"/>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>
<GroupBox Grid.Column="1" Header="مشخصات" Margin="10">
<StackPanel Margin="10">
<StackPanel Margin="1">
<Label FontSize="14" Margin="1">نام</Label>
<TextBox Name="txtName" Text="{Binding Name}" FontSize="16" Margin="1" Width="auto">
</TextBox>
</StackPanel>
</StackPanel>
</GroupBox>
</Grid>
</Window>
code-behind:
public partial class MainWindow : Window
{
public Task RootTask { get; set; }
TaskTreeViewModel vmTaskTree { get; set; }
public MainWindow()
{
InitializeComponent();
PrepareTree_Data();
}
public void PrepareTree_Data()
{
RootTask = SaveLoad.LoadData();
vmTaskTree = new TaskTreeViewModel(RootTask);
base.DataContext = vmTaskTree;
}
}