<navigation:Page xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"
x:Class="BusinessApplication1.Home"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation"
xmlns:Data="clr-namespace:System.Windows;assembly=System.Windows.Controls"
mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480"
Style="{StaticResource PageStyle}">
<Grid x:Name="LayoutRoot">
<Grid.Resources>
<Data:HierarchicalDataTemplate x:Key="UserGroupTemplate"
ItemsSource="{Binding Childern}">
<StackPanel Orientation="Horizontal"
Height="Auto" Width="Auto">
<TextBlock> <Run Text="{Binding Name}"></Run>(<Run Text="{Binding Type}"></Run>) </TextBlock>
</StackPanel>
</Data:HierarchicalDataTemplate>
<Style TargetType="sdk:TreeView" x:Key="UserGroupStyle">
<Setter Property="ItemTemplate"
Value="{StaticResource UserGroupTemplate}"/>
<Setter Property="BorderThickness" Value="1"/>
</Style>
</Grid.Resources>
<ScrollViewer x:Name="PageScrollViewer" Style="{StaticResource PageScrollViewerStyle}">
<StackPanel x:Name="ContentStackPanel" Style="{StaticResource ContentStackPanelStyle}">
<Grid>
<Grid.ColumnDefinitions >
<ColumnDefinition Width="250"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*"></RowDefinition>
<RowDefinition Height="30"></RowDefinition>
</Grid.RowDefinitions>
<sdk:TreeView x:Name="trv" SelectedItemChanged="trv_SelectedItemChanged"
ItemsSource="{Binding Items}"
Style="{StaticResource UserGroupStyle}"></sdk:TreeView>
<StackPanel Grid.Row="1" Orientation="Horizontal" >
<Button Content="+B" CommandParameter="Brand" Command="{Binding AddItemCMD}"></Button>
<Button Content="+L" CommandParameter="Product Line" Command="{Binding AddItemCMD}"></Button>
<Button Content="+P" CommandParameter="Product" Command="{Binding AddItemCMD}"></Button>
<Button Content="+T" CommandParameter="Task" Command="{Binding AddItemCMD}"></Button>
</StackPanel>
</Grid>
</StackPanel>
</ScrollViewer>
</Grid>
</navigation:Page>
namespace BusinessApplication1
{
using System.Windows.Controls;
using System.Windows.Navigation;
using System.Collections.ObjectModel;
using System;
using System.Windows;
using System.ComponentModel;
using System.Linq.Expressions;
using System.Windows.Input;
/// <summary>
/// Home page for the application.
/// </summary>
public partial class Home : Page
{
/// <summary>
/// Creates a new <see cref="Home"/> instance.
/// </summary>
public Home()
{
InitializeComponent();
this.Title = ApplicationStrings.HomePageTitle;
this.DataContext = VM ;
}
MainViewModel VM = new MainViewModel();
void trv_SelectedItemChanged(object sender, System.Windows.RoutedPropertyChangedEventArgs<object> e)
{
VM.SelectedItem = e.NewValue as NameValue;
}
/// <summary>
/// Executes when the user navigates to this page.
/// </summary>
protected override void OnNavigatedTo(NavigationEventArgs e)
{
}
}
public class MainViewModel : NotifyPropertyChangedBase
{
public MainViewModel()
{
addItemCMD = new XCommand(AddItem);
}
ObservableCollection<NameValue> items = new ObservableCollection<NameValue>();
public ObservableCollection<NameValue> Items { get { return items; } }
NameValue selectedItem;
public NameValue SelectedItem { get { return selectedItem; } set { selectedItem = value; RaisePropertyChanged(() => this.SelectedItem); } }
string type;
XCommand addItemCMD;
public XCommand AddItemCMD { get { return addItemCMD; } }
void AddItem(object p)
{
type = p as string;
BusinessApplication1.Views.AddItem add = new Views.AddItem();
add.Title = "Add " + type;
add.Closed += new EventHandler(add_Closed);
add.Show();
}
void add_Closed(object sender, EventArgs e)
{
BusinessApplication1.Views.AddItem add = sender as BusinessApplication1.Views.AddItem ;
if (add.DialogResult == true)
{
var nv = new NameValue { Name = add.name.Text, Type = type };
if (selectedItem == null)
{
items.Add(nv);
MessageBox.Show("Added " + type + " as Root");
}
else
{
selectedItem.Childern.Add(nv);
MessageBox.Show("Added " + type + " under " + selectedItem.Name);
}
}
else
{
MessageBox.Show("Discard changes !");
}
}
}
public class NameValue
{
public string Name { get; set; }
public string Value { get; set; }
public string Type { get; set; }
ObservableCollection<NameValue> childern = new ObservableCollection<NameValue>();
public ObservableCollection<NameValue> Childern { get { return childern; } }
public static ObservableCollection<NameValue> Get()
{
ObservableCollection<NameValue> list = new ObservableCollection<NameValue>();
Random r = new Random();
for (int i = 1; i < r.Next(5, 10); i++)
{
NameValue root1 = new NameValue { Name = "Node_" + i, Type = "Brand" };
list.Add(root1);
for (int j = 1; j < r.Next(5, 10); j++)
{
NameValue root2 = new NameValue { Name = "Node_" + i + "_" + j, Type = j % 3 == 1 ? "Key Word" : "Product Line" };
root1.Childern.Add(root2);
for (int l = 1; l < r.Next(5, 10); l++)
{
NameValue root3 = new NameValue { Name = "Node_" + i + "_" + j + "_" + l, Type = l % 3 == 1 ? "Key Word" : "Product" };
root2.Childern.Add(root3);
}
}
}
return list;
}
}
public class XCommand : ICommand //: ICommand
{
private Predicate<object> canExecute;
bool prevCanExe = true;
private Action<object> action;
public event EventHandler CanExecuteChanged;
public XCommand(Action<object> method)
: this(method, null)
{
}
public XCommand(Action<object> _action, Predicate<object> _canExecute)
{
action = _action;
canExecute = _canExecute;
prevCanExe = CanExecute(null);
}
public bool CanExecute(object parameter)
{
if (canExecute == null)
return true;
bool temp = canExecute(parameter);
if (temp != prevCanExe)
{
prevCanExe = temp;
if (CanExecuteChanged != null)
{
CanExecuteChanged(this, EventArgs.Empty);
}
}
return temp;
}
public void Execute(object parameter)
{
if (CanExecute(parameter) && action != null)
action(parameter);
}
}
public abstract class NotifyPropertyChangedBase : INotifyPropertyChanged
{
#region Constructor
protected NotifyPropertyChangedBase()
{
}
#endregion
#region Events
public event PropertyChangedEventHandler PropertyChanged;
#endregion
#region Event Handlers
/// <summary>
/// Get name of property
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="e"></param>
/// <returns></returns>
public static string GetPropertyName<T>(Expression<Func<T>> e)
{
var member = (MemberExpression)e.Body;
return member.Member.Name;
}
/// <summary>
/// Raise when property value propertychanged or override propertychage
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="propertyExpression"></param>
protected virtual void RaisePropertyChanged<T>(Expression<Func<T>> propertyExpression)
{
RaisePropertyChanged(GetPropertyName(propertyExpression));
}
/// <summary>
/// Raise when property value propertychanged
/// </summary>
/// <param name="propertyName"></param>
protected void RaisePropertyChanged(String propertyName)
{
PropertyChangedEventHandler temp = PropertyChanged;
if (temp != null)
{
temp(this, new PropertyChangedEventArgs(propertyName));
}
}
#endregion
}
}