WPF binds a data source to a TreeView control when the interface is stuck in a dead state

Source: Internet
Author: User

Let's start by talking about the basic idea of binding a custom class Treemode to a TreeView control, because each node contains a lot of custom property information, so we need to encapsulate that class of Treemode. The type of each node of the TreeView is Treemode, and we also define some children properties, which define the child and parent nodes of the current node, and of course define some common name, ToolTip, ID, Isexpand, IsChecked (mainly the addition of a checkbox in front of each node) and other attributes are some of the properties that need to be used, and the class implements the INotifyPropertyChanged interface, which provides a notification when the property changes. Update to the UI.

The other part is the foreground Code section, where some code is posted:

<TreeView.ItemTemplate>
<!--ItemsSource Specifies a subset of the data, that is, the next layer (hierarchicaldatatemplate.itemtemplate) shows those data--
<border name= "Fathernod" mouseleftbuttondown= "Fathernod_mouseleftbuttondown" >
<stackpanel orientation= "Horizontal" contextmenu= "{StaticResource sampleContextMenu1}" tooltip= "{Binding ToolTip } ">
<checkbox tag= "{Binding children}" ischecked= "{Binding IsChecked, Mode=twoway}" margin= "0,5,2,0" checked= "CheckBox _checked "/>
<image verticalalignment= "Center" source= "{Binding Icon}"/>
<stackpanel orientation= "Vertical" >
<textblock text= "{Binding cameraname}" horizontalalignment= "Center" width= "550"/>
<textbox x:name= "Renametextbox" text= "{Binding cameraname}" horizontalalignment= "Center" margin= "0,-20,0,0" Width = "550"
Visibility= "collapsed" lostfocus= "renametextbox_lostfous"/>
</StackPanel>
</StackPanel>
</Border>
<!--data binding with cascading data templates--
<HierarchicalDataTemplate.ItemTemplate>
<DataTemplate>
<border name= "Sonnod" mouseleftbuttondown= "Sonnod_mouseleftbuttondown" >
<stackpanel orientation= "Horizontal" contextmenu= "{StaticResource sampleContextMenu2}" tooltip= "{Binding ToolTip } ">
<checkbox tag= "{Binding children}" ischecked= "{Binding IsChecked, Mode=twoway}" margin= "0,5,2,0" checked= "CheckBox _checked "/>
<image verticalalignment= "Center" source= "{Binding Icon}"/>
<stackpanel orientation= "Vertical" >
<textblock text= "{Binding camerainstalladress}" horizontalalignment= "Center" width= "/>"
<textbox x:name= "Renametextbox" text= "{Binding cameraname}" horizontalalignment= "Center" margin= "0,-20,0,0" Width = "460" height= "40"
Visibility= "collapsed" lostfocus= "renametextbox_lostfous"/>
</StackPanel>
</StackPanel>
</Border>
</DataTemplate>
</HierarchicalDataTemplate.ItemTemplate>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>

The foreground code uses the Cascade data template to bind, the target of the binding is Treemode, and the other controls are bound to the corresponding property, which needs to be specifically explained, ItemsSource specifies a subset of that class of data, the next layer ( Hierarchicaldatatemplate.itemtemplate) Displays those data, which requires special attention. After you have completed these basic operations, you are adding a data source to the background code.

Here I am in two ways to add the data source, the first read the database after the data into the generic list, in this way there will be the interface card dead, here we define a data source property

Public list<treemode> Itemssourcedata
{
get {return itemssourcedata;}
Set
{
Itemssourcedata = value;
This. Allnodestreeview.itemssource = Itemssourcedata;
}
}

We encapsulate the data read from the database in a mylist=list<treemode>, where it is important to note that this must be the first time. Itemssourcedata=null, and then the this. ItemsSource Data=mylist, in order to update to the UI interface, and when added or deleted items, the interface will be stuck, the reaction speed is particularly slow, and then combined with some of the previous development experience, will list<treemode> changed to Observ Ablecollection<treemode>, and at the time of assignment, just

WPF binds a data source to a TreeView control when the interface is stuck in a dead state

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.