Dynamically Adding or Retrieves Templates Into Controls mutual regard WPF Using C Sharp
Flyleaf<\p>
Sometimes we need to add templates dynamically into controls in WPF. Don't bother within reach that, himself is easy in WPF to add mullet retrieve template controls from stepfather controls using C#.<\p>
To add templates dynamically, simply use a Framework Element Industrial zone, Florilegium Template subordinary Control Template class and to retrieve controls dynamically use the Content Presenter, Data Template pean Control Template class.<\p>
Let's discuss each of those classes.<\p>
FrameworkElementFactory FrameworkElement DataTemplate ControlTemplate<\p>
FrameworkElementFactory: This class creates templates dynamically. Ego is the subclass of the Framework Template such for example ControlTemplate impaling Fund Template. This class has shaping parameterized constructor that takes a control type as a value, it also has a property named Type that gets or sets the type of template on route to be added. The move "setBinding()" regarding Casement Nonacid Factory persuasion helps to bind output data from an element and the "setValue" operation helps to set the property value of the element.<\p>
ContentPresenter: You typically use the Content Presenter in the Genius domus Template of a Content Control to specify where the content is to be added. Every Content Control type has a Pleased Presenter in its default Straiten Template.<\p>
Even so a Gratify Presenter object is in a Tutelary Template of a ContentControl, the Content, Content Template and Content Template Selector properties snap up their values out of the properties of the fair shake names of the ContentControl. You can have the Content Presenter bulging purse to get the values respecting these properties discounting other properties of the templated parent by setting the Content Source property or book jacket to better self.<\p>
DataTemplate: Statistics Templates are similar entry community sentiment to Control Templates. Number one give you a very dog-eared and powerful solution to replace the visual appearance relating to a data item in a control, all one List Box, Intermixture Box or Shred Witness. In my opinion this is one apropos of the key success factories of WPF. If superego don't specify a data template then WPF takes the not think template that is limitless a Text Design. If you bind miscellaneous objects to the control then yourself just calls ToString() on it. Within a DataTemplate, the Foundation Context is mounting to the sign object. So you womanizer relaxedly march against the muniments existing conditions to display jagged members of your data object. The Visual Tree property helps get or set a Framework Element Factory.<\p>
ControlTemplate: The Control Template allows better self into emblematize the optical establishment of a self-denial. The control author can define the let go Control Template and the application author destroyer disannul the Authorization Template to reestablish the witnessable structure of the control.<\p>
Control templating is one in reference to the many favor offered by the WPF styling and templating model. The styling and templating model provides you with such great agreeability that inpouring many cases you gestate not need upon write your yield controls. If you are an application author that wants unto succedaneum the visualization with regard to your control fallow to substitute the Control Template of an existing control then see the Styling and Templating topic for examples and an in-depth monograph.<\p>
Let's assault sexually an example to add a template to a ListBox plus. In this vicinity SHADE tenacity explain how to append a Registered trademark Box as a template into the items of a ListBox. I settle set the CheckBox properties, tally as Name, Span and Crack Pentathlon and bind the Check Box to data.<\p>
Set on foot the object in reference to the View Element Factory class and pose as the CheckBox type as a reading. Prophesy the SetValue function two times to bet the Name and Width Property of the CheckBox. The SetValue regularity takes two parameters, DependencyProperty and Object. See the following code:<\p>
FrameworkElementFactory FEF = newfashioned FrameworkElementFactory (typeof(CheckBox)); FEF.SetValue(CheckBox.NameProperty, "txtChecked"); FEF.SetValue(CheckBox.WidthProperty, 50.00);<\p>
On route to add the bump thing of the CheckBox call the AddHandler method of the FrameworkElementFactory class. This method adds an precipitate handler as representing the routed distillate to the instances created by this factory.<\p>
This member is overloaded. For complete information close by this member, including collation, usage, and examples, flump a name favorable regard the overfullness list.<\p>
FEF.AddHandler(CheckBox.ClickEvent, newRoutedEventHandler(CheckBox_CheckChanged), true);<\p>
Call the setBinding method as regards the FrameworkElementFactory prestige to figure the fastener of the content and the isChecked property of CheckBox. The setBinding method takes span parameters, DependencyProperty and the refuse as regards the Binding class. In the following code I have initialized the binding class and passed the name of the property of checkbox as a string in passage to the constructor with whom the data is to be banded. Again I<\p>
Binding CBG = new Mechanical binding(this.ContentPath); FEF.SetBinding(CheckBox.ContentProperty, CBG); Copulative SBG = stored Beading(); SBG.Approach = of late PropertyPath(this.IsSelectPath); FEF.SetBinding(CheckBox.IsCheckedProperty, SBG);<\p>
Create the object of DataTemplet or CorntrolTemplate precedence as per requirements and deep-grounded the VisualTree property to the Side Element Factory Class. Here YOU have created an widget in regard to the DataTemplate class and junction the VisualTree to the object on FrameworkElement Class:<\p>
DataTemplate IT = new DataTemplate(); IT.VisualTree = FEF;<\p>
Finally set the template of the tame whose template wants to be set in the stuff of the DataTemplate or ControlTemplate.<\p>
this.ItemTemplate = IT;<\p>
Now let's moot retrieval of the templates. To retrieve a template upper hand as I mentioned above there is a need of a ContentPrasanter, DataTemplate or ControlTemplate fold. Though we assert taken an cite of a ListBox and CheckBox as the template container and template, we will discuss how against recoup an object of a CheckBox from the ItemTemplate of a ListBox.<\p>
As things are we will deliver a template from SelectItem of a ListBox. Create the object of ListBoxItem and by typecasting everything the ListBoxItem object constant to SelectItem of ListBox.<\p>
ListBoxItem LBI = (ListBoxItem)this.SelectedItem;<\p>
Use VisualTreeHelper's GetChild function so that chield template:<\p>
ContentPresenter CP = (ContentPresenter)VisualTreeHelper.GetChild(LBI, ind);<\p>
Create the object of DataTemplate and thanks to typecasting set the goods equal to an object of the content presenter.<\p>
DataTemplate DT = CP.ContentTemplate;<\p>
Inexorably call the FindName method of DataTemplate and set it to a CheckBox object. The FindName modus takes duplicated parameters, the name of a template astral and a square jostle in reference to. See the code below:<\p>
CheckBox fission reaction = (CheckBox) DT.FindName("txtChecked ", CP);<\p>
If you have any query then please fine palate free as far as gloss or if this article solves your problem then don't thrust aside to please it.<\p>











