Friday, April 20, 2007

Refering inner element through XAML

<StackPanel >
<TextBlock Text="{Binding ElementName=btn,Path=Foreground.Color.R}">
</TextBlock>
<Button Foreground="Red" Name="btn"/>

</StackPanel>

Get the template of 3rd party controls

If we dont have template of a Control we could get that using Blend.
Load the control in Blend and right click on it then select

Edit Control Parts(Template) -> Edit a Copy

This will create a copy of the template in xaml file.

Vista theme support in XP using WPF

This wont change the look and feel of the Window as in Vista ie the transparent window title.But changes the look and feel of the elements (Button,Combo etc) inside that Window.

http://blogs.msdn.com/llobo/archive/2006/12/13/Vista-look-on-Non_2D00_Aero-themes.aspx

Know the state of a object in XAML format at runtime

The below code gives the XAML representation of object at runtime.

MessageBox.Show(System.Windows.Markup.XamlWriter.Save(sp));

Using this we can save the current state of the objects..

Creating a control from xaml string at runtime

string s = "< Button xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' >";
s += "Hai";
s += " < /Button > ";

Stream strm = new MemoryStream(ASCIIEncoding.Default.GetBytes(s));
Button b = (Button)System.Windows.Markup.XamlReader.Load(strm);
stackPanel.Children.Add(B);

How can we make a TextBox looks like Button using template?

If we wanna make the Button looks like TextBox it is easy,just add the TextBox as content of Button.But the reverse is not possible sice TextBox doesnt have Content property.So use Templating.

< TextBox Text="Hai" >
< TextBox.Template >
< ControlTemplate >
< Button Content="hai"/ >
< /ControlTemplate >
< /TextBox.Template >
< /TextBox >

CS Code

ControlTemplate ct=new ControlTemplate(typeof(TextBox));
FrameworkElementFactory fef=new FrameworkElementFactory(typeof(Button));
fef.SetValue(Button.ContentProperty,"hai");
ct.VisualTree =fef;
txt.Template = ct;

Thursday, April 19, 2007

Start WPF 3D here

This helps us to understand what is Mesh,Normals etc and finally make us capable of creating a cube
http://www.kindohm.com/technical/WPF3DTutorial.htm

WPF 2D controls in 3D viewport

Get the library from below link which helps us to include 2d controls like Button,Textbox etc into 3D viewport.

http://codeplex.com/3DTools

Also this will raise all the events such as Click,MouseDown of 2D controls.

Applying same style to all wpf elements of same type

The folloging code will give a Red background to all the Buttons in the Window provided if we didnt set the ButtonBackground explicitly

< Window.Resources >
< Style x:key="{x:Type Button}" TargeType="{x:Type Button}" >
< Setter Property ="Background" Value ="Red"/ >
< /Style >
< /Window.Resources >



C# Code

Style sty = new Style(typeof(Button));

Setter set = new Setter(Button.BackgroundProperty , Brushes.Red);

sty.Setters.Add(set);

Resources.Add(typeof( Button), sty);

Welcome

Sharing My WPF experiences....
The posts will contain links more which are useful than my own articles :-)