Developing & deploying custom user controls in WPF

In WPF the development and deployment of custom user controls is similar to the development and deployment of user controls in ASP.NET. Let say that you have made a user control named MyUserCtrl1 in your main application project. In order to use this control in a Window you must first declare a namespace for it. The easy way to do this in the Window tag. After that the use of the control is as easy as writing a tag with the namespace that you have declared in the window tag and name – the name of the control:

XAML:

<Window x:Class="Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:uc="clr-namespace:MyApplication"
Title="Window1"
>
...
<uc:MyUserCtrl1 />
...
</Window>

Where MyApplication is the namespace of your application.
Now if the user control is in another assembly (for example MyApplication.GUI.dll) you must also type the assembly name in the XML namespace declaration:

XAML:

<Window x:Class="Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:uc="clr-namespace:MyApplication.GUI;assembly=MyApplication.GUI"
Title="Window1"
>
...
<uc:MyUserCtrl1 />
...
</Window>

Note that the assembly name may be different from the namespace!

How to close a tab with mouse middle click?

When I had to do this I’ve searched all over the Internet for some information on how to implement such a feature, but I have found nothing.
The main problem was how to detect on which tab exactly has the user clicked on. I haven’t found any method or property of the TabControl or TabPage controls in .NET Framework 2.0 that will be at some help. But after some experimenting I’ve found a very easy and effective way to do this. Here is the code:

VB:

Private Sub TabControl1_MouseUp(ByVal sender As Object _
, ByVal e As System.Windows.Forms.MouseEventArgs _
) Handles TabControl1.MouseUp
Dim nTabIndexAs Integer

'Detect on which tab the user has clicked
For nTabIndex= 0 To TabControl1.TabPages.Count - 1
If e.X >= TabControl1.GetTabRect(nTabIndex).X _
AndAlso e.X <= TabControl1.GetTabRect(nTabIndex).Width _
+ TabControl1.GetTabRect(nTabIndex).X _
Then
Exit For
End If
Next
If e.Button = Windows.Forms.MouseButtons.Middle Then
TabControl1.TabPages.RemoveAt(nTabIndex)
End If
End Sub

Password TextBox in WPF

In .NET Framework 3.0 (WPF) Microsoft have removed the property of a TextBox, which specifies whether the textbox is Password or Normal type. But in replacement they’ve introduced a new control named PasswordBox which is very simple and intuitive to use. Here is a simple example:

XAML:

<PasswordBox x:Name="txtPassword"
ToolTip="Password"
PasswordChar="*"
/>

By default the PasswordChar is set to the big black dot from Windows XP style logon and.
In order to detect changes in the password you must handle the PasswordChanged event:

VB Codebehind:

Private Sub txtPassword_PasswordChanged(ByVal sender As System.Object _
, ByVal e As System.Windows.RoutedEventArgs _
) Handles txtPassword.PasswordChanged
'Validate input
End Sub

An in order to retrieve the typed password you just use the Password property of the PasswordBox control:

VB Codebehind:

Public Function IsAuthenticated() as Boolean
Return txtPassword.Password <> ""
End Function