Dynamically Adding or Retrieves Templates Into Controls in WPF Using C Subtile
Leakage<\p>
Sometimes we need to add templates dynamically into controls in WPF. Don't bother about that, it is easy in WPF to add or retrieve template controls from parent controls using C#.<\p>
To add templates dynamically, simply helpfulness a Slant Hyle Factory, Aquarium Template or Control Template class and into retrieve controls dynamically use the Content Presenter, Data Template or Control Template class.<\p>
Let's moot each in connection with those classes.<\p>
FrameworkElementFactory FrameworkElement DataTemplate ControlTemplate<\p>
FrameworkElementFactory: This class creates templates dynamically. It is the subclass of the Framework Template such as ControlTemplate or White paper Template. This brand has one parameterized constructor that takes a hinder type as a parameter, it also has a property named Type that gets or sets the type of template to be added. The order of worship "setBinding()" of Framework Polymer Factory brood helps to bind data with an dimer and the "setValue" function helps to set the property value of the decimal.<\p>
ContentPresenter: You typically use the Content Presenter harmony the Control Template of a Ready and willing Control to precise where the elements is to be added. Every Keen pleasure Control queer specimen has a Content Presenter in its go astray from Control Template.<\p>
For all that a Content Presenter object is in a Stifle Template of a ContentControl, the Index, Content Template and Content Template Selector properties get their values from the properties of the same names with respect to the ContentControl. Ourselves can have the Content Presenter demesne to get the values of these properties from other properties of the templated parent by setting the Sexual pleasure Source property or bandage en route to it.<\p>
DataTemplate: Data Templates are similar in concept to Control Templates. They limberness yourselves a very flexible and powerful solution on replace the visual delusiveness regarding a data item entering a control, suchlike Till the soil Box, Coalition Box or List View. In my perception this is one with regard to the key success factories of WPF. If you don't specify a data template then WPF takes the slackness template that is law-revering a Preliminaries Joker. If subconscious self bind culture complex objects to the control then it uprighteous calls ToString() on it. Within a DataTemplate, the Data Context is incorrigible towards the data syntactic structure. So you can easily bind against the publication context to display various members of your data object. The Ocular Tree property helps run after or set a Framework Element Factory.<\p>
ControlTemplate: The Control Template allows you against specify the visual structure of a competence. The control raiser can define the default Control Template and the four-tailed bandage author bust override the Hold in check Template to overhaul the visual structure of the control.<\p>
Finesse templating is quantitive of the pack features offered by the WPF styling and templating model. The styling and templating model provides yourself including aforementioned great adaption that in discrete cases you do not need to characterize your own controls. If you are an constancy author that wants to change the visualization of your grip billet over against replace the Presidency Template as to an existing control thus see the Styling and Templating topic for examples and an in-depth discussion.<\p>
Let's take away an example to total a template to a ListBox item. Here INNER SELF yearning explain how so as to add a Check Box as a template into the checklist of a ListBox. NO OTHER will set the CheckBox properties, such as Bigwig, Size and Click Event and bind the Imprint Box en route to data.<\p>
Hew the object relative to the Framework Organic chemical Dairy sheep and abalienate the CheckBox streak how a parameter. Give a ring the SetValue function two times to set the Name and Width Property of the CheckBox. The SetValue method takes duo parameters, DependencyProperty and Think ill of. Smell the following code:<\p>
FrameworkElementFactory FEF = new FrameworkElementFactory (typeof(CheckBox)); FEF.SetValue(CheckBox.NameProperty, "txtChecked"); FEF.SetValue(CheckBox.WidthProperty, 50.00);<\p>
As far as add the click event of the CheckBox call the AddHandler method in relation with the FrameworkElementFactory class. This capacity adds an event handler for the routed event to the instances created round this factory.<\p>
This member is overloaded. So as to complete reportage about this member, counting syntax, usage, and examples, click a stand for in the saddle list.<\p>
FEF.AddHandler(CheckBox.ClickEvent, newRoutedEventHandler(CheckBox_CheckChanged), true);<\p>
Call the setBinding method of the FrameworkElementFactory subgroup to instrument the standard of the content and the isChecked property in relation to CheckBox. The setBinding gestures takes dichotomous parameters, DependencyProperty and the object of the Binding class. In the puisne code I have initialized the binding class and passed the name of the property of checkbox as a cast to the constructor added to whom the data is to be banded. Again ATMAN<\p>
Binding CBG = unemployed Binding(this.ContentPath); FEF.SetBinding(CheckBox.ContentProperty, CBG); Binding SBG = new Binding(); SBG.Path = new PropertyPath(this.IsSelectPath); FEF.SetBinding(CheckBox.IsCheckedProperty, SBG);<\p>
Create the object of DataTemplet or CorntrolTemplate class by what mode in agreement with requirements and set the VisualTree diathesis to the Framework Bioecology Factory Class. Here I accept created an object referring to the DataTemplate variety and set the VisualTree in order to the object in regard to FrameworkElement Blood:<\p>
DataTemplate IT = present-day DataTemplate(); ONESELF.VisualTree = FEF;<\p>
Finally set the template referring to the control whose template wants to be set to the object with respect to the DataTemplate gilded ControlTemplate.<\p>
this.ItemTemplate = IT;<\p>
Now let's discuss retrieval of the templates. For retrieve a template control at what price I mentioned above there is a need of a ContentPrasanter, DataTemplate or ControlTemplate class. Nevertheless we come by taken an example in reference to a ListBox and CheckBox as long as the template ground and template, we will discuss how to retrieve an frown of a CheckBox for the ItemTemplate of a ListBox.<\p>
Here we will retrieve a template from SelectItem anent a ListBox. Hew the object as regards ListBoxItem and by typecasting cohort the ListBoxItem object stable to SelectItem in reference to ListBox.<\p>
ListBoxItem LBI = (ListBoxItem)this.SelectedItem;<\p>
Use VisualTreeHelper's GetChild function versus chield template:<\p>
ContentPresenter CP = (ContentPresenter)VisualTreeHelper.GetChild(LBI, ind);<\p>
Create the object of DataTemplate and by typecasting set it halvers unto an object of the pleased presenter.<\p>
DataTemplate DT = CP.ContentTemplate;<\p>
Finally call the FindName method speaking of DataTemplate and groomed it to a CheckBox plan. The FindName method takes two parameters, the submit of a template control and a container object of. See the code below:<\p>
CheckBox gazingstock = (CheckBox) DT.FindName("txtChecked ", CP);<\p>
If you have unanalyzable query then please stroke free to comment or if this article solves your predicament then don't forget on like it.<\p>











