Przeglądaj źródła

listbox item布局为canvas,可以在鼠标point新建item,随意布局

tanghai 14 lat temu
rodzic
commit
55c4eb2815

+ 3 - 1
CSharp/Modules/BehaviorTree/BehaviorTree.csproj

@@ -68,7 +68,7 @@
     <Reference Include="WindowsBase" />
   </ItemGroup>
   <ItemGroup>
-    <Compile Include="BehaviorTreeModel.cs" />
+    <Compile Include="BehaviorTreeViewModel.cs" />
     <Compile Include="BehaviorTreeModule.cs" />
     <Compile Include="BehaviorTreeView.xaml.cs">
       <DependentUpon>BehaviorTreeView.xaml</DependentUpon>
@@ -78,6 +78,8 @@
       <DesignTime>True</DesignTime>
       <DependentUpon>Person.proto</DependentUpon>
     </Compile>
+    <Compile Include="TreeNode.cs" />
+    <Compile Include="TreeNodeViewModel.cs" />
   </ItemGroup>
   <ItemGroup>
     <Folder Include="Properties\" />

+ 0 - 18
CSharp/Modules/BehaviorTree/BehaviorTreeModel.cs

@@ -1,18 +0,0 @@
-using System.ComponentModel.Composition;
-using Microsoft.Practices.Prism.ViewModel;
-using NLog;
-using System.Windows.Input;
-using Microsoft.Practices.Prism.Commands;
-using System.Collections.ObjectModel;
-using System.Windows;
-using Google.ProtocolBuffers;
-
-namespace BehaviorTree
-{
-	[Export(typeof(BehaviorTreeViewModel))]
-	[PartCreationPolicy(CreationPolicy.NonShared)]
-	class BehaviorTreeViewModel : NotificationObject
-	{
-	}
-}
-

+ 13 - 26
CSharp/Modules/BehaviorTree/BehaviorTreeView.xaml

@@ -11,21 +11,6 @@
 		d:DesignHeight="600" d:DesignWidth="800">
 
 	<UserControl.Resources>
-		<DataTemplate x:Key="ContentListBoxItemTemplate">
-			<Border CornerRadius="15" Width="150" Margin="3" Height="300">
-				<Border.Background>
-					<LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
-						<GradientStop Color="OrangeRed" Offset="1" />
-						<GradientStop Color="Brown" Offset="0" />
-					</LinearGradientBrush>
-				</Border.Background>
-			</Border>
-		</DataTemplate>
-
-		<Style TargetType="ListBoxItem">
-			<Setter Property="Canvas.Left" Value="{Binding X}"/>
-			<Setter Property="Canvas.Top" Value="{Binding Y}"/>
-		</Style>
 	</UserControl.Resources>
 	<UserControl.CommandBindings>
 		<CommandBinding Command="ApplicationCommands.New" Executed="NewNode_Executed" />
@@ -38,21 +23,23 @@
 		</ContextMenu>
 	</UserControl.ContextMenu>
 	<Grid>
-		<ListBox ItemTemplate="{StaticResource ContentListBoxItemTemplate}" >
+		<ListBox Name="lstTree" ItemsSource="{Binding TreeNodes}" >
 			<ListBox.ItemsPanel>
 				<ItemsPanelTemplate>
-					<Canvas />
+					<Canvas Background="Green"/>
 				</ItemsPanelTemplate>
 			</ListBox.ItemsPanel>
-			<!--<ListBoxItem>One</ListBoxItem>
-			<ListBoxItem Canvas.Left="50">Two</ListBoxItem>
-			<ListBoxItem Canvas.Left="100">Three</ListBoxItem>
-			<ListBoxItem Canvas.Top="50" Canvas.Left="40">Four</ListBoxItem>
-			<ListBoxItem Canvas.Bottom="100" Canvas.Left="0">Five</ListBoxItem>
-			<ListBoxItem Canvas.Top="100" Canvas.Left="40">Six</ListBoxItem>
-			<ListBoxItem Canvas.Top="100" Canvas.Left="80">Seven</ListBoxItem>
-			<ListBoxItem Canvas.Top="100" Canvas.Left="120">Eight</ListBoxItem>
-			<ListBoxItem Canvas.Top="100" Canvas.Left="160">Nine</ListBoxItem>-->
+			<ListBox.ItemContainerStyle>
+				<Style>
+					<Setter Property="Canvas.Left" Value="{Binding Mode=TwoWay, Path=X}"/>
+					<Setter Property="Canvas.Top" Value="{Binding Mode=TwoWay, Path=Y}"/>
+				</Style>
+			</ListBox.ItemContainerStyle>
+			<ListBox.ItemTemplate>
+				<DataTemplate>
+					<TextBlock FontWeight="Bold" TextAlignment="Center" Text="Node" />
+				</DataTemplate>
+			</ListBox.ItemTemplate>
 		</ListBox>
 	</Grid>
 </UserControl>

+ 3 - 1
CSharp/Modules/BehaviorTree/BehaviorTreeView.xaml.cs

@@ -43,7 +43,9 @@ namespace BehaviorTree
 
 		private void NewNode_Executed(object sender, ExecutedRoutedEventArgs e)
 		{
-
+			Point point = Mouse.GetPosition(lstTree);
+			var treeNode = new TreeNode(point.X, point.Y);
+			this.ViewModel.Add(treeNode, null);
 		}
 	}
 }

+ 51 - 0
CSharp/Modules/BehaviorTree/BehaviorTreeViewModel.cs

@@ -0,0 +1,51 @@
+using System.ComponentModel.Composition;
+using Microsoft.Practices.Prism.ViewModel;
+using NLog;
+using System.Windows.Input;
+using Microsoft.Practices.Prism.Commands;
+using System.Collections.ObjectModel;
+using System.Windows;
+using Google.ProtocolBuffers;
+
+namespace BehaviorTree
+{
+	[Export(typeof(BehaviorTreeViewModel))]
+	[PartCreationPolicy(CreationPolicy.NonShared)]
+	class BehaviorTreeViewModel
+	{
+		private ObservableCollection<TreeNodeViewModel> treeNodes = new ObservableCollection<TreeNodeViewModel>();
+
+		public ObservableCollection<TreeNodeViewModel> TreeNodes
+		{
+			get
+			{
+				return treeNodes;
+			}
+			set
+			{
+				treeNodes = value;
+			}
+		}
+
+		public void Add(TreeNode treeNode, TreeNodeViewModel parent)
+		{
+			var treeNodeViewModel = new TreeNodeViewModel(treeNode, parent);
+			treeNodes.Add(treeNodeViewModel);
+			if (parent != null)
+			{
+				parent.Children.Add(treeNodeViewModel);
+			}
+		}
+
+		public void Remove(TreeNodeViewModel treeNodeViewModel)
+		{
+			for (int i = 0; i < treeNodeViewModel.Children.Count; ++i)
+			{
+				Remove(treeNodeViewModel.Children[0]);
+			}
+			treeNodeViewModel.Parent.Children.Remove(treeNodeViewModel);
+			treeNodes.Remove(treeNodeViewModel);
+		}
+	}
+}
+

+ 13 - 1
CSharp/Modules/BehaviorTree/TreeNodeViewModel.cs

@@ -16,7 +16,7 @@ namespace BehaviorTree
 		public TreeNodeViewModel(TreeNode treeNode, TreeNodeViewModel parent)
 		{
 			this.treeNode = treeNode;
-			this.parent = parent;
+			this.parent = parent ?? this;
 		}
 
 		public double X
@@ -70,6 +70,18 @@ namespace BehaviorTree
 			}
 		}
 
+		public TreeNodeViewModel Parent
+		{
+			get
+			{
+				return parent;
+			}
+			set
+			{
+				parent = value;
+			}
+		}
+
 		public ObservableCollection<TreeNodeViewModel> Children
 		{
 			get