Click here to Skip to main content
15,885,309 members
Home / Discussions / WPF
   

WPF

 
GeneralRe: Cannot set a property on object 'Identity' because it is in a read-only state. Pin
Pete O'Hanlon9-Jul-09 4:38
mvePete O'Hanlon9-Jul-09 4:38 
GeneralRe: Cannot set a property on object 'Identity' because it is in a read-only state. Pin
ASysSolvers9-Jul-09 5:03
ASysSolvers9-Jul-09 5:03 
QuestionPropertyChanged Nofification Pin
schiebel-t9-Jul-09 1:22
schiebel-t9-Jul-09 1:22 
AnswerRe: PropertyChanged Nofification Pin
Pete O'Hanlon9-Jul-09 1:38
mvePete O'Hanlon9-Jul-09 1:38 
GeneralRe: PropertyChanged Nofification Pin
up_late21-Jul-09 13:33
up_late21-Jul-09 13:33 
QuestionWPF Toolkit: DataGrid disbled the cell Pin
laprathab9-Jul-09 0:15
laprathab9-Jul-09 0:15 
Questiondefault control template xaml Pin
Bob Bedell8-Jul-09 13:28
Bob Bedell8-Jul-09 13:28 
AnswerRe: default control template xaml Pin
Kamal Gurnani9-Jul-09 1:06
Kamal Gurnani9-Jul-09 1:06 
The WPF documentation doesn’t list the XAML for standard control templates. But, you can write program to get the control template and then modify it and Re-Apply it.


Here is the program to get the control Template Markup of a given control.

In a Main window take a listbox control to enlist all the controls, and when any item is selected the corrosponding Control Template's Markup is displayed in a Textblock control.

Here is the WPF program to demonstrate the same

This is the XAML part
<Window x:Class="DefaultControlTemplate.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="462" Width="572" Loaded="Window_Loaded" >
<Grid>
<ListBox Margin="12,12,0,12" Name="listBox1" HorizontalAlignment="Left" Width="168" SelectionChanged="listBox1_SelectionChanged" />
<TextBlock HorizontalAlignment="Right" Margin="0,14,12,10" Name="textBlock1" Width="159" />
</Grid>
</Window>



This is how .cs file would look like

private void Window_Loaded(object sender, RoutedEventArgs e)
{
Type controlType = typeof(Control);
List<Type> ctrlTypes = new List<Type>();


//searches all the types in a dll where generic control class is defined

Assembly assembly = Assembly.GetAssembly(typeof(Control));

foreach (Type type in assembly.GetTypes())
{
// Only add a type of the list if it's a Control, a concrete class,
// and public.
if (type.IsSubclassOf(controlType) && !type.IsAbstract && type.IsPublic)
{
ctrlTypes.Add(type);

}
}

listBox1.ItemsSource = ctrlTypes;

}

------------------------------------------------------------

private void listBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{

try
{

// Get the selected type.
Type type = (Type)listBox1.SelectedItem;

// Instantiate the type.
ConstructorInfo info = type.GetConstructor(System.Type.EmptyTypes);
Control control = (Control)info.Invoke(null);


// Get the template.
ControlTemplate template = control.Template;

// Get the XAML for the template.
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
StringBuilder sb = new StringBuilder();
XmlWriter writer = XmlWriter.Create(sb, settings);
XamlWriter.Save(template, writer);
textBlock1.Text = sb.ToString();


}

catch (Exception err)
{
textBlock1.Text = "<< Error generating template: " + err.Message + ">>";
}
}
---------------------------------------------------------

Let me explain the code:

At the Window_loaded event, I have enlisted all the controls in a list box.

When any of the items from list box is selected, I have created the control as per selection, got it's control template and serialized into XML and displayed the same in textblock as text.


If you want, you can start with a simpler program, say a window has a button and a textblock. When clicked on button,
the corrosponding XAML for button's control template will be displayed in textblock. In this case, Button is instantiated, hence need to created Button Object, as shown below.



private void button1_Click(object sender, RoutedEventArgs e)
{
ControlTemplate template = ((Control)sender).Template;
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
StringBuilder sb = new StringBuilder();
XmlWriter writer = XmlWriter.Create(sb, settings);
XamlWriter.Save(template, writer);
label1.Content = sb.ToString();


}
GeneralRe: default control template xaml Pin
Bob Bedell9-Jul-09 12:02
Bob Bedell9-Jul-09 12:02 
GeneralRe: default control template xaml Pin
Bob Bedell9-Jul-09 12:04
Bob Bedell9-Jul-09 12:04 
GeneralRe: default control template xaml Pin
Bob Bedell9-Jul-09 16:12
Bob Bedell9-Jul-09 16:12 
GeneralRe: default control template xaml Pin
Bob Bedell11-Jul-09 4:08
Bob Bedell11-Jul-09 4:08 
Questiontextbox - select all Pin
teejayem8-Jul-09 6:07
teejayem8-Jul-09 6:07 
AnswerRe: textbox - select all Pin
User 2710099-Jul-09 18:23
User 2710099-Jul-09 18:23 
QuestionMessage Removed Pin
8-Jul-09 3:45
professionalN_tro_P8-Jul-09 3:45 
AnswerRe: WPF Charting Pin
Pete O'Hanlon8-Jul-09 4:41
mvePete O'Hanlon8-Jul-09 4:41 
QuestionListview binding is not working with page class Pin
laprathab7-Jul-09 21:50
laprathab7-Jul-09 21:50 
AnswerRe: Listview binding is not working with page class Pin
User 2710097-Jul-09 22:05
User 2710097-Jul-09 22:05 
GeneralRe: Listview binding is not working with page class Pin
laprathab7-Jul-09 23:11
laprathab7-Jul-09 23:11 
GeneralRe: Listview binding is not working with page class Pin
User 2710097-Jul-09 23:17
User 2710097-Jul-09 23:17 
GeneralRe: Listview binding is not working with page class Pin
laprathab7-Jul-09 23:32
laprathab7-Jul-09 23:32 
GeneralRe: Listview binding is not working with page class Pin
User 2710097-Jul-09 23:37
User 2710097-Jul-09 23:37 
GeneralRe: Listview binding is not working with page class Pin
laprathab7-Jul-09 23:47
laprathab7-Jul-09 23:47 
GeneralRe: Listview binding is not working with page class Pin
User 2710097-Jul-09 23:51
User 2710097-Jul-09 23:51 
GeneralRe: Listview binding is not working with page class Pin
laprathab7-Jul-09 23:55
laprathab7-Jul-09 23:55 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.