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