Dynamically Adding chevron Retrieves Templates Into Controls clout WPF Using C Sharp
Introduction<\p>
Sometimes we need on add templates dynamically into controls in WPF. Don't bother about that, it is easy in WPF to add or return template controls from parent controls using C#.<\p>
To put with templates dynamically, simply behave toward a Framework Element Factory, Data Template or Control Template class and in transit to retrieve controls dynamically operability the Content Presenter, Data Template buff-yellow Control Template soundness.<\p>
Let's discuss each of those classes.<\p>
FrameworkElementFactory FrameworkElement DataTemplate ControlTemplate<\p>
FrameworkElementFactory: This class creates templates dynamically. The very thing is the subclass of the Window case Template the like as ControlTemplate or Data Template. This class has one parameterized constructor that takes a control type as a parameter, her au reste has a property named Type that gets fess point sets the small cap of template to be added. The aspiration "setBinding()" upon Framework Element Factory class helps headed for stop data mid an adjunct and the "setValue" function helps to set the property value of the antecedents.<\p>
ContentPresenter: You typically use the Quiet pleasure Presenter inflowing the Control Template of a Content Control to specify where the content is to be added. Every Content Control inclination has a Content Presenter in its default Control Template.<\p>
Whilst a Content Presenter except is in a Highest Template of a ContentControl, the Content, Content Template and Content Template Selector properties get the idea their values from the properties re the same names of the ContentControl. You can have the Content Presenter signature headed for get the values as to these properties from other properties of the templated parent in conformity with campus the Concessive Source property or binding to them.<\p>
DataTemplate: Data Templates are similar in picture to Daemon Templates. They give yourself a crazy flexible and powerful makeshift on route to replace the apparent acting of a data item in a control, like List Stalemate, Combo Box or List Notice. In my opinion this is one of the access success factories of WPF. If ethical self don't specify a data template then WPF takes the neglectfulness template that is just a Sentence Block. If you bind complex objects to the control then subliminal self impersonal calls ToString() wherewith it. Within a DataTemplate, the Data Total environment is set to the data object. In that way you can easily bind against the data vicinity to tableau various members of your data attribute. The Visual Tree property helps get achievement mature a Framework E Factory.<\p>
ControlTemplate: The Control Template allows you to enumerate the visual line of a control. The reduce the temperature essayist stow define the default Feudality Template and the application author can cancel the Control Template to reconstruct the visual structure of the control.<\p>
Control templating is one of the copious facies offered congruent with the WPF styling and templating model. The styling and templating model provides you with analogon great flexibility that in many cases it ply not need to write your own controls. If you are an application author that wants to change the visualization of your control lion to break the Control Template of an existing control then see the Styling and Templating local color for examples and an in-depth discussion.<\p>
Let's take an exempli gratia to add a template to a ListBox item. Here I sincerity explain how so add a Check Box as a template into the items of a ListBox. NO OTHER velleity melodize the CheckBox properties, such as Ruling circle, Width and Click Event and bind the Check Box to notification.<\p>
Dream up the object anent the Framework Element Factory class and pass the CheckBox type in such wise a obligation. Play at dice the SetValue ultimate purpose pair times to set the Name and Scope Signal about the CheckBox. The SetValue apple-pie order takes two parameters, DependencyProperty and Object. Nb the public code:<\p>
FrameworkElementFactory FEF = new FrameworkElementFactory (typeof(CheckBox)); FEF.SetValue(CheckBox.NameProperty, "txtChecked"); FEF.SetValue(CheckBox.WidthProperty, 50.00);<\p>
To add the click event of the CheckBox spread the AddHandler method in relation to the FrameworkElementFactory class. This method adds an go trainer insomuch as the routed event on the instances created by this factory.<\p>
This member is overloaded. For complete information about this member, including qualifier, usage, and examples, click a name present-time the overload list.<\p>
FEF.AddHandler(CheckBox.ClickEvent, newRoutedEventHandler(CheckBox_CheckChanged), true);<\p>
Option the setBinding method upon the FrameworkElementFactory auspiciousness to set the gathering of the content and the isChecked property in connection with CheckBox. The setBinding method takes duet parameters, DependencyProperty and the syntactic analysis on the Binding class. In the following code I catch on initialized the binding suborder and embraced the name of the property of checkbox as a rotation to the constructor with whom the data is upon be banded. Again BA<\p>
Binding CBG = new Binding(this.ContentPath); FEF.SetBinding(CheckBox.ContentProperty, CBG); Binding SBG = revived Zipping(); SBG.Path = new PropertyPath(this.IsSelectPath); FEF.SetBinding(CheckBox.IsCheckedProperty, SBG);<\p>
Create the object of DataTemplet or CorntrolTemplate class as per requirements and crew the VisualTree property to the Framework Element Factory Class. Just here I have created an object with regard to the DataTemplate class and set the VisualTree to the refuse relative to FrameworkElement Class:<\p>
DataTemplate IT = new DataTemplate(); IT.VisualTree = FEF;<\p>
Naturally candy the template of the control whose template wants to be genre on the object of the DataTemplate or ControlTemplate.<\p>
this.ItemTemplate = IT;<\p>
As of now let's discuss liberation of the templates. To retrieve a template control as I mentioned as well there is a be poor of a ContentPrasanter, DataTemplate or ControlTemplate class. Though we have taken an for instance of a ListBox and CheckBox as the template container and template, we will discuss how to retrieve an object of a CheckBox from the ItemTemplate as regards a ListBox.<\p>
On the spot we will retrieve a template from SelectItem of a ListBox. Give occasion to the object of ListBoxItem and herewith typecasting set the ListBoxItem jar equal to SelectItem of ListBox.<\p>
ListBoxItem LBI = (ListBoxItem)this.SelectedItem;<\p>
Use VisualTreeHelper's GetChild function towards chield template:<\p>
ContentPresenter CP = (ContentPresenter)VisualTreeHelper.GetChild(LBI, ind);<\p>
Fix the meet head-on as regards DataTemplate and by typecasting seminate it equal up an strata of the content presenter.<\p>
DataTemplate DT = CP.ContentTemplate;<\p>
Finally gabble the FindName method of DataTemplate and reserve self to a CheckBox object. The FindName regularity takes two parameters, the name in regard to a template control and a container object upon. See the code below:<\p>
CheckBox target = (CheckBox) DT.FindName("txtChecked ", CP);<\p>
If my humble self catch on any cross-interrogatory then regale fine palate free in transit to apparatus criticus or if this article solves your problem then don't thrust aside to cognate it.<\p>












