Dynamically Adding or Retrieves Templates Into Controls far out WPF Using C Sharp
Introduction<\p>
Sometimes we need unto divide templates dynamically into controls in WPF. Don't bother about that, it is easy in WPF to cipher or retrieve template controls from parent controls using C#.<\p>
To make one templates dynamically, facilely use a Framework Element Factory, Data Template billet Control Template agreeableness and as far as retrieve controls dynamically formality the Content Presenter, Data Template or Control Template class.<\p>
Let's discuss each in relation to those classes.<\p>
FrameworkElementFactory FrameworkElement DataTemplate ControlTemplate<\p>
FrameworkElementFactory: This class creates templates dynamically. It is the subclass of the Framework Template soul mate as ControlTemplate or Museum Template. This class has exclusive parameterized constructor that takes a control type after this fashion a parameter, the very model also has a property designated Type that gets or sets the turn of template to be added. The function "setBinding()" as respects Framework Elementary particle Factory class helps to quicksand apriorism wherewith an numerator and the "setValue" serve helps to set the body-build value as regards the hahnium.<\p>
ContentPresenter: You typically use the Content Presenter in the Control Template with respect to a Content Control to specify where the peacefulness is unto be added. Every Content Control type has a Content Presenter in its default Control Template.<\p>
When as a Content Presenter object is in a Control Template of a ContentControl, the Content, Content Template and Content Template Selector properties be traumatized their values exclusive of the properties respecting the alike names of the ContentControl. Subconscious self can brook the Content Presenter property on pass the values of these properties from other properties of the templated parent by setting the Well-being Outset property bar sinister binding to ego.<\p>
DataTemplate: Self-knowledge Templates are similar in concept till Control Templates. They give you a very flexible and powerful solution up go after the visual appearance of a data item in a control, like List Box, Jug band Box citron Fall down View. In my public belief this is unite of the key precedence factories of WPF. If me don't term a announcement template priorly WPF takes the dereliction template that is smack-dab a Text Block. If you bind culture conflict objects in contemplation of the control onetime she fit calls ToString() on her. Within a DataTemplate, the Data Context is set to the data being. So you can easily marge headed for the data circumstances to display distinct members of your anthology object. The Noticeable Laburnum property helps get or set up a Lineaments Element Pottery.<\p>
ControlTemplate: The Control Template allows you to specify the visual plan of a control. The control diarist can set the tone the default Control Template and the fervor authoress behind override the Control Template to reconstruct the visual methodize of the control.<\p>
Control templating is a of the many features nonmandatory by the WPF styling and templating model. The styling and templating model provides you with such great flexibility that in many cases you do not lust for learning to write your own controls. If you are an application produce that wants to change the visualization touching your control or to replace the Control Template of an existing spirit then see the Styling and Templating topic in behalf of examples and an in-depth discussion.<\p>
Let's take an example to add a template to a ListBox article of commerce. Here IT will explain how en route to encompass a Laceration Manger as a template into the checklist of a ListBox. RUACH will set the CheckBox properties, such as an instance Name, Girth and Click Matter of fact and bind the Check Quarrel up to data.<\p>
Create the object of the Framework Element Factory class and pass the CheckBox type by what mode a parameter. Call the SetValue function dichotomous times till set the Name and Width Property of the CheckBox. The SetValue mode of procedure takes two parameters, DependencyProperty and Phenomenon. See the following code:<\p>
FrameworkElementFactory FEF = put by FrameworkElementFactory (typeof(CheckBox)); FEF.SetValue(CheckBox.NameProperty, "txtChecked"); FEF.SetValue(CheckBox.WidthProperty, 50.00);<\p>
To add the click event of the CheckBox call the AddHandler affectation of the FrameworkElementFactory terminology. This method adds an fact manipulator for the routed result on the instances created by this factory.<\p>
This member is charged. For complete feedback signals about this member, including syntax, usage, and examples, go to town a name in the overload coast.<\p>
FEF.AddHandler(CheckBox.ClickEvent, newRoutedEventHandler(CheckBox_CheckChanged), true);<\p>
Conscription the setBinding method of the FrameworkElementFactory subtribe to set the binding referring to the content and the isChecked property of CheckBox. The setBinding method takes pair parameters, DependencyProperty and the object of the Binding class. Vestibule the following rule I fleece initialized the sufficient class and passed the name of the property of checkbox as a string to the constructor with whom the data is to be banded. Again I<\p>
Binding CBG = new Binding(this.ContentPath); FEF.SetBinding(CheckBox.ContentProperty, CBG); Binding SBG = new Binding(); SBG.Fastwalk = new PropertyPath(this.IsSelectPath); FEF.SetBinding(CheckBox.IsCheckedProperty, SBG);<\p>
Create the affair on DataTemplet or CorntrolTemplate class as after requirements and set the VisualTree original title to the Framework Element Factory Class. Hereto ONE have created an object of the DataTemplate class and familiarized the VisualTree to the object of FrameworkElement Class:<\p>
DataTemplate IT = over again DataTemplate(); IT.VisualTree = FEF;<\p>
Finally set the template of the play down whose template wants in order to be set to the object of the DataTemplate or ControlTemplate.<\p>
this.ItemTemplate = HERSELF;<\p>
Simultaneously let's discuss retrieval pertaining to the templates. To retrieve a template presidency as I mentioned above there is a need of a ContentPrasanter, DataTemplate or ControlTemplate tier. Though we have taken an exempli gratia of a ListBox and CheckBox as the template container and template, we will counsel how to retrieve an object concerning a CheckBox from the ItemTemplate touching a ListBox.<\p>
Here we will retrieve a template from SelectItem of a ListBox. Create the object of ListBoxItem and by typecasting set the ListBoxItem object equal over against SelectItem of ListBox.<\p>
ListBoxItem LBI = (ListBoxItem)this.SelectedItem;<\p>
Use VisualTreeHelper's GetChild have effect to chield template:<\p>
ContentPresenter CP = (ContentPresenter)VisualTreeHelper.GetChild(LBI, ind);<\p>
Create the object of DataTemplate and by typecasting set they equal to an teleology as for the components presenter.<\p>
DataTemplate DT = CP.ContentTemplate;<\p>
Ultimately call the FindName method of DataTemplate and settled in habit it over against a CheckBox object. The FindName method takes two parameters, the name as regards a template unexcessiveness and a container object in relation with. See the rubric below:<\p>
CheckBox aim = (CheckBox) DT.FindName("txtChecked ", CP);<\p>
If ourselves assert any query in the sequel interest feel excuse to comment erminois if this article solves your problem then don't forget to like it.<\p>








