Kaynağa Gözat

绑定成功,可以显示root节点

tanghai 14 yıl önce
ebeveyn
işleme
938b21611a

+ 1 - 1
CSharp/CSharp.sln

@@ -3,7 +3,7 @@ Microsoft Visual Studio Solution File, Format Version 11.00
 # Visual Studio 2010
 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "View", "View\View.csproj", "{FA1C18E4-C928-4900-A77B-6944B3443B7E}"
 EndProject
-Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Model", "Data\Model.csproj", "{3978E513-87FA-44EC-893A-DBD90A5A8808}"
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Model", "Model\Model.csproj", "{3978E513-87FA-44EC-893A-DBD90A5A8808}"
 EndProject
 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ViewModel", "ViewModel\ViewModel.csproj", "{E55C43C7-4CE2-4384-AE8A-39E074F2FC0B}"
 EndProject

+ 0 - 0
CSharp/Data/BehaviorNode.cs → CSharp/Model/BehaviorNode.cs


+ 3 - 1
CSharp/Data/Model.csproj → CSharp/Model/Model.csproj

@@ -41,7 +41,9 @@
   </ItemGroup>
   <ItemGroup>
     <Compile Include="BehaviorNode.cs" />
-    <Compile Include="Properties\AssemblyInfo.cs" />
+  </ItemGroup>
+  <ItemGroup>
+    <Folder Include="Properties\" />
   </ItemGroup>
   <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
   <!-- To modify your build process, add your task inside one of the targets below and uncomment it. 

+ 8 - 4
CSharp/View/BehaviorTreeView.xaml

@@ -3,13 +3,17 @@
 		xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 		xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
 		xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
+		xmlns:vm="clr-namespace:Egametang;assembly=ViewModel"
 		mc:Ignorable="d" 
 		d:DesignHeight="300" d:DesignWidth="300">
+	<UserControl.DataContext>
+		<vm:BehaviorTreeViewModel />
+	</UserControl.DataContext>
 	<Grid>
 		<TreeView Grid.Column="0"  HorizontalAlignment="Stretch" Name="behaviorTreeView" 
 				VerticalAlignment="Stretch" Padding="0" BorderThickness="0" 
 				ContextMenuOpening="behaviorTreeView_ContextMenuOpening"
-				ItemsSource="{Binding Children}">
+				ItemsSource="{Binding Path=Children}" >
 			<TreeView.ContextMenu>
 				<ContextMenu>
 					<MenuItem Name="newMenuItem" Header="New" Click="newMenuItem_Click" />
@@ -19,7 +23,7 @@
 				</ContextMenu>
 			</TreeView.ContextMenu>
 
-			<TreeView.ItemContainerStyle>
+			<!--<TreeView.ItemContainerStyle>
 				<Style TargetType="{x:Type TreeViewItem}">
 					<Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}" />
 					<Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />
@@ -30,9 +34,9 @@
 						</Trigger>
 					</Style.Triggers>
 				</Style>
-			</TreeView.ItemContainerStyle>
+			</TreeView.ItemContainerStyle>-->
 			<TreeView.ItemTemplate>
-				<HierarchicalDataTemplate ItemsSource="{Binding Children}">
+				<HierarchicalDataTemplate ItemsSource="{Binding Children}" >
 					<TextBlock Text="{Binding Name}" />
 				</HierarchicalDataTemplate>
 			</TreeView.ItemTemplate>

+ 2 - 13
CSharp/View/BehaviorTreeView.xaml.cs

@@ -1,16 +1,5 @@
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Text;
-using System.Windows;
+using System.Windows;
 using System.Windows.Controls;
-using System.Windows.Data;
-using System.Windows.Documents;
-using System.Windows.Input;
-using System.Windows.Media;
-using System.Windows.Media.Imaging;
-using System.Windows.Navigation;
-using System.Windows.Shapes;
 
 namespace Egametang
 {
@@ -27,7 +16,7 @@ namespace Egametang
 		private void newMenuItem_Click(object sender, RoutedEventArgs e)
 		{
 
-		};
+		}
 
 		private void copyMenuItem_Click(object sender, RoutedEventArgs e)
 		{

+ 2 - 2
CSharp/View/MainWindow.xaml

@@ -1,7 +1,7 @@
 <Window x:Class="Egametang.MainWindow"
 		xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 		xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
-		xmlns:e="clr-namespace:Egametang"
+		xmlns:v="clr-namespace:Egametang"
 		Title="MainWindow" Height="800" Width="1280">
 	<Grid>
 		<Grid.RowDefinitions>
@@ -20,7 +20,7 @@
 				<ColumnDefinition Width="Auto" />
 				<ColumnDefinition Width="*" />
 			</Grid.ColumnDefinitions>
-			<e:BehaviorTreeView />
+			<v:BehaviorTreeView />
 			<GridSplitter Grid.Column="1" HorizontalAlignment="Center" VerticalAlignment="Stretch" Width="5" />
 			<Canvas />
 		</Grid>

+ 0 - 4
CSharp/View/MainWindow.xaml.cs

@@ -1,6 +1,4 @@
 using System.Windows;
-using System.Windows.Controls;
-using System.Windows.Input;
 using System.Collections;
 
 namespace Egametang
@@ -10,8 +8,6 @@ namespace Egametang
 	/// </summary>
 	public partial class MainWindow : Window
 	{
-		private Hashtable treeViewNodes = new Hashtable();
-
 		public MainWindow()
 		{
 			InitializeComponent();

+ 11 - 1
CSharp/View/View.csproj

@@ -9,7 +9,7 @@
     <OutputType>WinExe</OutputType>
     <AppDesignerFolder>Properties</AppDesignerFolder>
     <RootNamespace>Egametang</RootNamespace>
-    <AssemblyName>GameEditor</AssemblyName>
+    <AssemblyName>View</AssemblyName>
     <TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
     <TargetFrameworkProfile>Client</TargetFrameworkProfile>
     <FileAlignment>512</FileAlignment>
@@ -81,6 +81,16 @@
   <ItemGroup>
     <Folder Include="Properties\" />
   </ItemGroup>
+  <ItemGroup>
+    <ProjectReference Include="..\Model\Model.csproj">
+      <Project>{3978E513-87FA-44EC-893A-DBD90A5A8808}</Project>
+      <Name>Model</Name>
+    </ProjectReference>
+    <ProjectReference Include="..\ViewModel\ViewModel.csproj">
+      <Project>{E55C43C7-4CE2-4384-AE8A-39E074F2FC0B}</Project>
+      <Name>ViewModel</Name>
+    </ProjectReference>
+  </ItemGroup>
   <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
   <!-- To modify your build process, add your task inside one of the targets below and uncomment it. 
        Other similar extension points exist, see Microsoft.Common.targets.

+ 75 - 10
CSharp/ViewModel/BehaviorNodeViewModel.cs

@@ -1,18 +1,34 @@
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Text;
+using System.Collections.ObjectModel;
+using System.ComponentModel;
 
 namespace Egametang
 {
-	public class BehaviorNodeViewModel : TreeViewItemViewModel
+	public class BehaviorNodeViewModel : INotifyPropertyChanged
 	{
-		private readonly BehaviorNode node;
+		private BehaviorNode node;
+		private BehaviorNodeViewModel parent;
+		private ObservableCollection<BehaviorNodeViewModel> children = 
+			new ObservableCollection<BehaviorNodeViewModel>();
 
-		public BehaviorNodeViewModel(BehaviorNode node, BehaviorNodeViewModel parent) :
-			base(parent, false)
+		private bool isExpanded;
+		private bool isSelected;
+
+		public BehaviorNodeViewModel(BehaviorNode node, BehaviorNodeViewModel parent)
 		{
 			this.node = node;
+			this.parent = parent;
+		}
+
+		public ObservableCollection<BehaviorNodeViewModel> Children
+		{
+			get
+			{
+				return children;
+			}
+			set
+			{
+				children = value; 
+			}
 		}
 
 		public string Name
@@ -23,11 +39,60 @@ namespace Egametang
 			}
 		}
 
-		public virtual void LoadChildren()
+		public bool IsSelected
+		{
+			get 
+			{ 
+				return isSelected; 
+			}
+			set
+			{
+				if (value != isSelected)
+				{
+					isSelected = value;
+					this.OnPropertyChanged("IsSelected");
+				}
+			}
+		}
+
+		public bool IsExpanded
+		{
+			get 
+			{ 
+				return isExpanded; 
+			}
+			set
+			{
+				if (value != isExpanded)
+				{
+					isExpanded = value;
+					this.OnPropertyChanged("IsExpanded");
+				}
+
+				if (isExpanded && parent != null)
+				{
+					parent.IsExpanded = true;
+				}
+
+				this.LoadChildren();
+			}
+		}
+
+		protected void LoadChildren()
 		{
 			foreach (var child in node.Children)
 			{
-				base.Children.Add(new BehaviorNodeViewModel(child, this));
+				children.Add(new BehaviorNodeViewModel(child, this));
+			}
+		}
+
+		public event PropertyChangedEventHandler PropertyChanged;
+
+		protected virtual void OnPropertyChanged(string propertyName)
+		{
+			if (this.PropertyChanged != null)
+			{
+				this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
 			}
 		}
 	}

+ 37 - 0
CSharp/ViewModel/BehaviorTreeViewModel.cs

@@ -0,0 +1,37 @@
+using System.Collections.ObjectModel;
+using System.ComponentModel;
+
+namespace Egametang
+{
+	public class BehaviorTreeViewModel : INotifyPropertyChanged
+	{
+		private BehaviorNode root = new BehaviorNode();
+		private ObservableCollection<BehaviorNodeViewModel> children =
+			new ObservableCollection<BehaviorNodeViewModel>();
+
+		public BehaviorTreeViewModel()
+		{
+			root.Name = "root";
+			root.Type = 1;
+			children.Add(new BehaviorNodeViewModel(root, null));
+		}
+
+		public ObservableCollection<BehaviorNodeViewModel> Children
+		{
+			get
+			{
+				return children;
+			}
+		}
+
+		public event PropertyChangedEventHandler PropertyChanged;
+
+		protected virtual void OnPropertyChanged(string propertyName)
+		{
+			if (this.PropertyChanged != null)
+			{
+				this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
+			}
+		}
+	}
+}

+ 0 - 174
CSharp/ViewModel/TreeViewItemViewModel.cs

@@ -1,174 +0,0 @@
-using System.Collections.ObjectModel;
-using System.ComponentModel;
-
-namespace Egametang
-{
-	/// <summary>
-	/// Base class for all ViewModel classes displayed by TreeViewItems.  
-	/// This acts as an adapter between a raw data object and a TreeViewItem.
-	/// </summary>
-	public class TreeViewItemViewModel : INotifyPropertyChanged
-	{
-		#region Data
-
-		static readonly TreeViewItemViewModel dummyChild = new TreeViewItemViewModel();
-
-		readonly ObservableCollection<TreeViewItemViewModel> children;
-		readonly TreeViewItemViewModel parent;
-
-		bool isExpanded;
-		bool isSelected;
-
-		#endregion // Data
-
-		#region Constructors
-
-		protected TreeViewItemViewModel(TreeViewItemViewModel parent, bool lazyLoadChildren)
-		{
-			this.parent = parent;
-
-			children = new ObservableCollection<TreeViewItemViewModel>();
-
-			if (lazyLoadChildren)
-			{
-				children.Add(dummyChild);
-			}
-		}
-
-		// This is used to create the DummyChild instance.
-		private TreeViewItemViewModel()
-		{
-		}
-
-		#endregion // Constructors
-
-		#region Children
-
-		/// <summary>
-		/// Returns the logical child items of this object.
-		/// </summary>
-		public ObservableCollection<TreeViewItemViewModel> Children
-		{
-			get 
-			{
-				return children; 
-			}
-		}
-
-		#endregion // Children
-
-		#region HasLoadedChildren
-
-		/// <summary>
-		/// Returns true if this object's Children have not yet been populated.
-		/// </summary>
-		public bool HasDummyChild
-		{
-			get 
-			{ 
-				return this.Children.Count == 1 && this.Children[0] == dummyChild; 
-			}
-		}
-
-		#endregion // HasLoadedChildren
-
-		#region IsExpanded
-
-		/// <summary>
-		/// Gets/sets whether the TreeViewItem 
-		/// associated with this object is expanded.
-		/// </summary>
-		public bool IsExpanded
-		{
-			get 
-			{ 
-				return isExpanded; 
-			}
-			set
-			{
-				if (value != isExpanded)
-				{
-					isExpanded = value;
-					this.OnPropertyChanged("IsExpanded");
-				}
-
-				// Expand all the way up to the root.
-				if (isExpanded && parent != null)
-				{
-					parent.IsExpanded = true;
-				}
-
-				// Lazy load the child items, if necessary.
-				if (this.HasDummyChild)
-				{
-					this.Children.Remove(dummyChild);
-					this.LoadChildren();
-				}
-			}
-		}
-
-		#endregion // IsExpanded
-
-		#region IsSelected
-
-		/// <summary>
-		/// Gets/sets whether the TreeViewItem 
-		/// associated with this object is selected.
-		/// </summary>
-		public bool IsSelected
-		{
-			get 
-			{ 
-				return isSelected; 
-			}
-			set
-			{
-				if (value != isSelected)
-				{
-					isSelected = value;
-					this.OnPropertyChanged("IsSelected");
-				}
-			}
-		}
-
-		#endregion // IsSelected
-
-		#region LoadChildren
-
-		/// <summary>
-		/// Invoked when the child items need to be loaded on demand.
-		/// Subclasses can override this to populate the Children collection.
-		/// </summary>
-		protected virtual void LoadChildren()
-		{
-		}
-
-		#endregion // LoadChildren
-
-		#region Parent
-
-		public TreeViewItemViewModel Parent
-		{
-			get 
-			{ 
-				return parent; 
-			}
-		}
-
-		#endregion // Parent
-
-		#region INotifyPropertyChanged Members
-
-		public event PropertyChangedEventHandler PropertyChanged;
-
-		protected virtual void OnPropertyChanged(string propertyName)
-		{
-			if (this.PropertyChanged != null)
-			{
-				this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
-			}
-		}
-
-		#endregion // INotifyPropertyChanged Members
-	}
-}

+ 5 - 3
CSharp/ViewModel/ViewModel.csproj

@@ -41,15 +41,17 @@
   </ItemGroup>
   <ItemGroup>
     <Compile Include="BehaviorNodeViewModel.cs" />
-    <Compile Include="Properties\AssemblyInfo.cs" />
-    <Compile Include="TreeViewItemViewModel.cs" />
+    <Compile Include="BehaviorTreeViewModel.cs" />
   </ItemGroup>
   <ItemGroup>
-    <ProjectReference Include="..\Data\Model.csproj">
+    <ProjectReference Include="..\Model\Model.csproj">
       <Project>{3978E513-87FA-44EC-893A-DBD90A5A8808}</Project>
       <Name>Model</Name>
     </ProjectReference>
   </ItemGroup>
+  <ItemGroup>
+    <Folder Include="Properties\" />
+  </ItemGroup>
   <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
   <!-- To modify your build process, add your task inside one of the targets below and uncomment it. 
        Other similar extension points exist, see Microsoft.Common.targets.