首页 > 代码库 > TreeView 数据绑定及选中命令处理

TreeView 数据绑定及选中命令处理

昨天接近下班,一个群里面的网友,问treeView绑定后  选中命令怎么来处理,怎么没有效果,而且用MVVM的方式来写;快下班了本来想远程帮他看下,结果就说写个Demo给他看;再加上选中传参;

下面分享下代码:

 

技术分享
 1 <TreeView Height="264" x:Name="tvProperties"  Width="220" ItemsSource="{Binding MyProperty}"><!--数据源--> 2             <i:Interaction.Triggers> 3                 <i:EventTrigger EventName="SelectedItemChanged"> 4                     <i:InvokeCommandAction Command="{Binding DataContext.SelectComamnd, 5                             RelativeSource={RelativeSource AncestorType=Window, AncestorLevel=1,  Mode=FindAncestor}}" CommandParameter="{Binding SelectedItem, ElementName=tvProperties}"/><!--选中传值--> 6                 </i:EventTrigger> 7             </i:Interaction.Triggers> 8             <TreeView.ItemContainerStyle> 9                 <Style TargetType="{x:Type TreeViewItem}">10                     <Setter Property="IsExpanded" Value="{Binding IsExpanded}"></Setter>11                 </Style>12             </TreeView.ItemContainerStyle>13             <TreeView.ItemTemplate>14 15                 <HierarchicalDataTemplate  ItemsSource="{Binding Path=Children}"><!--节点-->16 17                     <StackPanel Orientation="Horizontal">18 19                         <Image VerticalAlignment="Center" Source="{Binding Icon}" Width="16" Height="16" Margin="0,0,2,2"></Image><!--绑定值-->20 21                         <TextBlock VerticalAlignment="Center" Name="nodeName" Text="{Binding Name}"></TextBlock>22 23                         <Image VerticalAlignment="Center" Source="{Binding EditIcon}" Margin="2,0,0,0"></Image>24 25                         <StackPanel.ToolTip>26 27                             <TextBlock VerticalAlignment="Center" Text="{Binding Name}" TextWrapping="Wrap" MaxWidth="200" ></TextBlock>28 29                         </StackPanel.ToolTip>30 31                     </StackPanel>32 33                 </HierarchicalDataTemplate>34 35             </TreeView.ItemTemplate>36           37         </TreeView>
View Code

这个绑定完了 咱们来看看数据:

技术分享
 1    private List<PropertyNodeItem> myVar; 2         /// <summary> 3         /// 绑定值 4         /// </summary> 5         public List<PropertyNodeItem> MyProperty 6         { 7             get { return myVar ?? (myVar = new List<PropertyNodeItem>()); } 8             set 9             {10                 myVar = value;11                 this.OnPropertyChanged("MyProperty");12             }13         }14         /// <summary>15         /// 产生数据16         /// </summary>17        public  MainViewModels()18        {19            for (int i = 0; i < 10; i++)20            {21                PropertyNodeItem aa = new PropertyNodeItem()22                {23                    Name="测试"+i,24                    Children = new List<PropertyNodeItem>() {  new PropertyNodeItem(){Name="测试1"+i+i} }25                };26                MyProperty.Add(aa);27            }28        }
ViewModel Code

接着命令代码:

技术分享
 1     private RelayCommand _selectCommand; 2        /// <summary> 3        /// 事件命令 4        /// </summary> 5        public RelayCommand SelectComamnd 6        { 7            get { return _selectCommand ?? (_selectCommand = new RelayCommand(OnSelectComamnd)); } 8            set { _selectCommand = value; } 9        }10        /// <summary>11        /// 选中事件执行函数12        /// </summary>13        /// <param name="paramter">参数</param>14        private void OnSelectComamnd(object paramter)15        {16 17        }
ViewModel Code

完毕~~~

 

TreeView 数据绑定及选中命令处理