TreeView Data binding and check command handling

Source: Internet
Author: User

Yesterday close to work, a group inside of the Netizen, asked the TreeView binding after the check command how to deal with, how no effect, and with MVVM way to write; get off work. Originally wanted to help him to see the remote, the result is said to write a demo to him to see;

Share the following code:

1 <TreeViewHeight= " the"x:name= "Tvproperties"Width= "The "ItemsSource="{Binding MyProperty}"><!--Data Source -2             <i:interaction. Triggers>3                 <I:eventtriggerEventName= "Selecteditemchanged">4                     <i:invokecommandactionCommand="{Binding datacontext.selectcomamnd,5 Relativesource={relativesource Ancestortype=window, ancestorlevel=1, mode=findancestor}}< /c4>"CommandParameter="{Binding SelectedItem, elementname=tvproperties}"/><!--Select the value to pass -6                 </I:eventtrigger>7             </i:interaction. Triggers>8             <Treeview.itemcontainerstyle>9                 <StyleTargetType="{x:type TreeViewItem}">Ten                     <Setter Property= "isexpanded"Value="{Binding isexpanded}"></Setter> One                 </Style> A             </Treeview.itemcontainerstyle> -             <treeview.itemtemplate> -  the                 <hierarchicaldatatemplateItemsSource="{Binding Path=children}"><!--node - -  -                     <StackPanelOrientation= "Horizontal"> -  +                         <ImageVerticalAlignment= "Center"Source="{Binding Icon}"Width= "+"Height= "+"Margin= "0,0,2,2"></Image><!--Bound Value - -  +                         <TextBlockVerticalAlignment= "Center"Name= "NodeName"Text="{Binding Name}"></TextBlock> A  at                         <ImageVerticalAlignment= "Center"Source="{Binding Editicon}"Margin= "2,0,0,0"></Image> -  -                         <Stackpanel.tooltip> -  -                             <TextBlockVerticalAlignment= "Center"Text="{Binding Name}"textwrapping= "Wrap"MaxWidth= "$" ></TextBlock> -  in                         </Stackpanel.tooltip> -  to                     </StackPanel> +  -                 </hierarchicaldatatemplate> the  *             </treeview.itemtemplate> $           Panax Notoginseng         </TreeView>
View Code

This binding is over. Let's look at the data:

1    PrivateList<propertynodeitem>MyVar;2         /// <summary>3         ///Bound Value4         /// </summary>5          PublicList<propertynodeitem>MyProperty6         {7             Get{returnMyVar?? (MyVar =NewList<propertynodeitem>()); }8             Set9             {TenMyVar =value; One                  This. OnPropertyChanged ("MyProperty"); A             } -         } -         /// <summary> the         ///Generate Data -         /// </summary> -         Publicmainviewmodels () -        { +             for(inti =0; I <Ten; i++) -            { +Propertynodeitem AA =NewPropertynodeitem () A                { atName="Test"+I, -Children =NewList<propertynodeitem> () {NewPropertynodeitem () {name="Test 1"+i+i}} -                }; - myproperty.add (AA); -            } -}
ViewModel Code

Then the command code:

1     PrivateRelaycommand _selectcommand;2        /// <summary>3        ///Event Command4        /// </summary>5         PublicRelaycommand selectcomamnd6        {7            Get{return_selectcommand?? (_selectcommand =NewRelaycommand (ONSELECTCOMAMND)); }8            Set{_selectcommand =value;}9        }Ten        /// <summary> One        ///Check Event execution function A        /// </summary> -        /// <param name= "paramter" >Parameters</param> -        Private voidONSELECTCOMAMND (Objectparamter) the        { -  -}
ViewModel Code

Finished ~ ~ ~

TreeView Data binding and check command handling

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.