Create a Login Window in WPF
Creating the Window
To start, you need to create a window with no border and can be made into any
shape you want. To do this you will set a few different attributes. The WindowStyle
attribute normally allows you to set a single border, three-D border, or a Tool
Window border. Setting this attribute to None will eliminate the border. The
ShowInTaskbar attribute is optional, but if you are building a login screen you
probably won’t want this window to show up in the Task Bar as it is going to be
modal style form. The next two attributes, AllowsTransparency and Background
work together. You must set AllowsTransparency to True to allow the Background
to be set to Transparent. If you do not set these two attributes, then your border will
still show up. Below is the xaml for this window.
<Window ...
WindowStartupLocation="CenterScreen"
AllowsTransparency="True"
ShowInTaskBar=False
Background="Transparent"
WindowStyle="None"
SizeToContent="WidthAndHeight"
FocusManager.FocusedElement=
"{Binding ElementName=txtUserName}">
...
...
</Window>
There are three additional attributes that are set on this window. The
WindowStartupLocation attribute is set to “CenterScreen” to ensure that the login
screen is displayed in the middle of the screen when it is shown. You also set the
SizeToContent attribute to WidthAndHeight to just take as much room for this
window as the controls need that are contained within this window. The
FocusManager.FocusedElement attribute is data-bound to the textbox control next
to the User Name label. This tells WPF to place the cursor in this textbox once the
screen is displayed.
The Border
Now that you have the Window xaml defined you now can create the look for the
outside border of the window. A Border control is used to form the outside of this
login screen. You will set the CornerRadius attribute to “10” to give the nice
rounded corners. You can set the BorderBrush to “Gray” and the BorderThickness
to “3”. You also want to give this border a nice wide Margin to allow room for the
2 Create a Login Window in WPF
Copyright © 2009 by PDSA, Inc.
All rights reserved. Reproduction is strictly prohibited.
- 1
- 2
- 3
前往页