Click here to Skip to main content
15,879,535 members
Articles / Desktop Programming / WPF
Tip/Trick

Some WPF Tip/Tricks For the Beginners

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
28 May 2011CPOL 19.3K   1   1
For the souls confused about WPF.
Accessing the Global variables in one Window/Page from another in WPF.
This was the first problem I found in my migration from WinForms to WPF.

In WinForms, what we usually do is

VB
frmFormName.GlobalVar1=value 'From another form


In WPF, this fails miserably.

The workaround is
VB
'In the required window - say MainWindow class
Dim VarToChange as Integer 'Can be any type.

Public Property VarNameDesc as Integer 'Type should be same as VarToChange's Type
Get

Return VarToChange

End Get

Set(ByVal value As Integer) 'Here too.

VarToChange=value

End Set


Now, you can access/change it from another window by using MainWindow.VarNameDesc.

Using a UserControl UC1 from your existing project
Adding a custom control or UserControl to the Toolbox has the following limitations.

Works only for custom controls defined outside the current project.

Does not update correctly when you change the solution configuration from Debug to Release, or from Release to Debug. This is because the reference is not a project reference, but is for the assembly on disk instead. If the control is part of the current solution, when you change from Debug to Release, your project continues to reference the Debug version of the control.

In addition, if design-time metadata is applied to the custom control and this metadata specifies that the ToolboxBrowsableAttribute is set to false, the control does not appear in the Toolbox. 


Source : http://msdn.microsoft.com/en-us/library/bb514128.aspx[^]

After some meddling with the XAML File, the workaround was adding this to the Window tag :
XML
xmlns:my="clr-namespace:ProjectName"

Actually, a dropdown will let you choose.

Then, I found this link (Wish I had seen it before!) : http://msdn.microsoft.com/en-us/library/bb514546.aspx[^].

The Obsolete OuterGlow BitmapEffect
Visual Studio warned me that BitmapEffect became Obsolete. I wanted the OuterGlow effect actually. The DropShadowEffect with ShadowDepth set to 0 did the job.
VB
Dim og As New DropShadowEffect
og.ShadowDepth = 0
og.BlurRadius = 11 '10-14 do a good job.
og.Color = Colors.Gold
btnDemo.Effect = og 'A Button's Name


Using Windows Forms Controls
I actually needed a DateTimePicker (WPF in .NET 3.0). Since it is introduced only in later versions, I used the WindowsFormsHost control.

XML
<windowsformshost name="wfh">
<wf:datetimepicker visible="True" name="dtFinally" xmlns:wf="#unknown">
</wf:datetimepicker></windowsformshost>


Typing <wf: will automatically show the list.
Make sure to include the following in the Window tag :
XML
xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Student
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralReason for my vote of 5 It helped me on adding a DateTimePic... Pin
caterdog15-Feb-12 19:48
caterdog15-Feb-12 19:48 

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.