The document discusses various controls available in WPF like Button, Border, Label, TextBox, and Canvas. It provides code examples and descriptions for how to use each control, including setting properties like content, background, foreground, borders, and event handlers. Specific controls and properties discussed include the Button click event, Border thickness and brush, adding content to labels, text wrapping in text boxes, and using coordinates to position elements in a Canvas.
Download as DOCX, PDF, TXT or read online on Scribd
100%(1)100% found this document useful (1 vote)
261 views
WPF Windows Based Controls
The document discusses various controls available in WPF like Button, Border, Label, TextBox, and Canvas. It provides code examples and descriptions for how to use each control, including setting properties like content, background, foreground, borders, and event handlers. Specific controls and properties discussed include the Button click event, Border thickness and brush, adding content to labels, text wrapping in text boxes, and using coordinates to position elements in a Canvas.
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 76
WPF Windows Based Controls
1. Button Control In WPF
2. Border Control In WPF 3. Label Control in WPF 4. TextBox Control In WPF 5. Canvas Control In WPF 6. CheckBox Control In WPF 7. ComboBox In WPF 8. Stack Panel Control 9. Expander Control 10. DockPanel Control 11. Document Viewer Control 12. Ellipse Control 13. Content Control 14. Frame Control 15. GroupBox Control 16. Grid Control 17. Progressbar Control 18. Menu Control 19. ListView Control 20. Adding Item in ListView Dynamically 21. Formatting And Style Of ListView 22. RadioButton Control 23. Tab Control 24. PasswordBox Control 25. Scroll Viewer Control 26. Slider Control 27. ViewBox Control 28. Separator Control 29. TreeView Control 30. UniformGrid Control 31. WrappedPanel Control 32. Image Control 33. Rectangle Control
Button Control In WPF The Button element represents a WPF Button control in XAML. <Button></Button> The Width and Height attributes of the Button element represent the width and the height of a Button. The Content property of the Button element sets the text of a button. The x:Name attribute represents the name of the control, which is a unique identifier of a control.
Adding a Button Click Event Handler The Click attribute of the Button element adds the click event handler. The following code adds the click event handler for a Button.
private void button1_Click(object sender, RoutedEventArgs e) { Random generator = new Random(); int randomValue; randomValue = generator.Next(1, 10); textBlock1.Text += " " + randomValue.ToString(); }
Properties of Button Name Description AllowDrop Gets or sets a value indicating whether this element can be used as the target of a drag-and- drop operation. AreAnyTouchesCaptured Gets a value that indicates whether at least one touch is captured to this element. AreAnyTouchesCapturedWithin Gets a value that indicates whether at least one touch is captured to this element or to any child elements in its visual tree. AreAnyTouchesDirectlyOver Gets a value that indicates whether at least one touch is pressed over this element. AreAnyTouchesOver Gets a value that indicates whether at least one touch is pressed over this element or any child elements in its visual tree. CacheMode Gets or sets a cached representation of the UIElement. ClickMode Gets or sets when the Click event occurs. Clip Gets or sets the geometry used to define the outline of the contents of an element. ClipToBounds Gets or sets a value indicating whether to clip the content of this element (or content coming from the child elements of this element) to fit into the size of the containing element. Command Gets or sets the command to invoke when this button is pressed. CommandBindings Gets a collection of CommandBinding objects associated with this element. A CommandBinding enables command handling for this element, and declares the linkage between a command, its events, and the handlers attached by this element. CommandParameter Gets or sets the parameter to pass to the Command property. CommandTarget Gets or sets the element on which to raise the specified command. Content Gets or sets the content of a ContentControl. ContentStringFormat Gets or sets a composite string that specifies how to format the Content property if it is displayed as a string. ContentTemplate Gets or sets the data template used to display the content of the ContentControl. ContentTemplateSelector Gets or sets a template selector that enables an application writer to provide custom template- selection logic. InputBindings Gets the collection of input bindings associated with this element. InputScope Gets or sets the context for input used by this FrameworkElement.
Border Control In WPF The controls in WPF do not have border property so WPF provides a border control. Similar to other WPF elements, the Border has Width, Height, Background, and Horizontal Alignment and Vertical Alignment properties. Besides these common properties, Border has two properties that make Border a border. BorderThickness -The BorderThickness property represents the thickness of the border. BorderBrush.- The BorderBrush property represents the brush that is used to draw the border. The Corner Radius property represents the degree to which the corners of a Border are rounded.
Adding Contents to a Label Control The Content property of the Label control allows you to set any other controls as the content of a Label control. The code snippet in Listing 3 adds some ellipse controls to a Label control.
Formatting a Label The BorderBrush property of the Label sets a brush to draw the border of a Label. You may use any brush to fill the border. Setting Image as Background of a Label
Setting Background and Foreground Colors The Background and Foreground attributes set the background and foreground colors of text box.
<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Window1" Height="300" Width="300"> <Grid> <TextBox Width="200" Height="40" Canvas.Top="50" Canvas.Left="20" Background="Red" Foreground="Yellow"> This is TextBox !! </TextBox> </Grid> </Window>
Output-
Wrapping and Scrolling Text The TextWrapping attributes sets the wrapping of text and VerticalScrollBarVisibility and HorizontalScrollBarVisibility sets the vertical and horizontal scroll bars visible.
Restricting Input Text MaxHeight, MaxWidth, MaxLines, and MaxLength attributes of text box restricts the maximum height, maximum width, maximum number of lines, and maximum length of the text box. Similarly MinHeight, MinWidht, MinLines, and MinLength restricts the minimum height, minimum width, minimum number of lines, and minimum length of the text box. Setting IsReadOnly attribute to true makes the text box non editable. Canvas Control In WPF A Canvas panel is used to position child elements by using coordinates that are relative to the canvas area. Here are some of the properties of Canvas panels. The default values of Height and Width properties of a Canvas are 0. If you do not set these values, you will not see a canvas unless child elements are automatically resizable. Child elements on a Canvas are never resized. The vertical and horizontal alignments on child elements do not work. Child elements are placed on positions set by the Canvas Left, Top, Right, and Bottom properties. Margin does work partially. If Left property of Canvas is set, Right property does not work. If Top property of Canvas is set, Bottom property does not work. Properties The Canvas control has three properties. The Left property represents the distance between the left side of a control and its parent container Canvas. The Top property represents the distance between the top of a control and its parent container Canvas.
The Content attribute represents the text of a CheckBox.
The Name attribute represents the name of the control, which is a unique identifier of a control.
The Foreground attribute defines the foreground color of the text of the CheckBox.
The Content attribute defines the text of the CheckBox.
FontFamily, FontStyle, FontWeight, FontSize and FontStretch are font related attribute
The IsChecked property represents the state of the CheckBox control. The IsThreeState property represents whether the CheckBox has two or three states. Three states are checked, unchecked, or indeterminate. Code for CheckBox Control is given Below-
Adding Events on CheckBox The Checked and Unchecked attributes of the CheckBox element adds the checked and unchecked event handler. These events are fired when a CheckBox state is changed to checked and unchecked respectively.
ComboBox In WPF The Width and Height properties represent the width and the height of a ComboBox. The x:Name property represents the name of the control, which is a unique identifier of a control. The Margin property sets the location of a ComboBox on the parent control. The HorizontalAlignment and VerticalAlignment properties are used to set horizontal and vertical alignments.
As with the other layout panels, the StackPanel has a collection of Children that it literally shoves one after the other. You set the orientation to either horizontal or vertical to control where the items go. As shown in picture above. You might use the StackPanel if you have a series of controls with a set width or height that you want to show up in a row. For example, you might use StackPanel when you have a series of controls in a side panel (like the accordion control in Microsoft Outlookyou can expand/contract sections for mail, calendar, tasks, and so on). The controls can all change size, and the other controls will automatically be moved to accommodate the space. Code for Stack Panel
Adding scrolling support Adding scrolling is easy, and, once you get used to the idea of control composition, fairly intuitive. We simply put the StackPanel (or anything else we want to automatically scroll) inside a ScrollViewer. Code for this
Expander Control in WPF StackPanel is with expanding panelswhen the panels expand or contract, everything else changes size automatically. The WPF team has been nice enough to provide an expanding/contracting control which makes this behavior easy to demonstrate. The Expander control works similarly to the sections in Windows Explorer that allow you to show or hide different sections
<Expander Header="My Computers" BorderThickness="3" BorderBrush="AntiqueWhite" Background="Chocolate"> <StackPanel> <Button>Local Disk C:</Button> <Button>Local Disk D:</Button> <Button>Local Disk E:</Button> <Button>Local Disk F:</Button> <Button>Local Disk G:</Button> <Button>Local Disk H:</Button> </StackPanel> </Expander> </StackPanel>
</ScrollViewer>
The Expander can only contain one thing: its contents. If we want to add multiple items, we have to put something inside the Expander that can hold some number of other things. The StackPanel, as with all the other layout panels, can hold multiple items, so we can add another StackPanel. The StackPanel itself solves some specific layout scenarios, but its quite flexible. It could be used, for example, to build a calculator. You should be able to see how this would work one vertical StackPanel containing a number of vertical StackPanels for the buttons. It wouldnt be easy, but it would be possible. In the next section, we will talk about the DockPanel. The DockPanel can be used to solve some of the same problems but in a different way. As with the StackPanel, the DockPanel, while flexible, is designed to handle a different set of scenarios. The DockPanel layout in WPF A DockPanel is useful when you want to position various elements on the edges of your window. For example, you might put a menu and a toolbar at the top, an explorer bar at the left, and a status bar at the bottom. The remaining space would contain the main content with which the user interacts. As with a StackPanel, if you put a series of items on the same side, they will stack one after the other. In fact, if you add all the items at the top or the left, the behavior will be similar to that of a StackPanel.
<Expander Header="Less useful"></Expander> <Expander Header="Silly"></Expander> </StackPanel> <Button Padding="10 10"> <TextBlock TextWrapping="Wrap" TextAlignment="Center">This is all of the remaining space that is not docked</TextBlock> </Button> </DockPanel>
Document Viewer Control in WPF The DocumentViewer provides a very easy way to display the contents of a document, edit a template's form fields and to navigate around a document within a web browser. The server component returns standard web file formats (HTML, CSS, JS and JPG etc.), thus documents can be displayed in the browser without having to install any third party plugins, nor extensions. WPF does not support functionality to view Microsoft Word documents but there is a work around this problem. WPF DocumentViewer control is used to display fixed documents such as an XPS (XML Paper Specification) document. We can open a Word document if we can convert a Word document to an XPS document. This conversion is possible by using Office Interop and Office Tools frameworks that is used to work with Office documents. Add Reference to XPS and Office Interop Assemblies Before we do any actual work, we must add reference to the following assemblies.
The first assembly, ReachFramework.dll hosts the functionality for XPS documents and rest of the assemblies hosts the functionality Office Interop and Office Tools support. To add reference to these assemblies, you right click on Add Reference on the project name in Solution Explorer. On the .NET Framework, select ReachFramework and other assemblies from the list and click OK button. a sshown in Figure below-
You may have multiple assemblies installed on your machine. Make sure you select Version 12 for Microsoft.Office.Interop.Word assemblies as you see in Figure bellow, otherwise your conversion will fail.
Once you have added the reference to assemblies, you must import the following namespaces to your code behind.
using System.IO; using Microsoft.Office.Interop.Word; using Microsoft.Win32; using System.Windows.Xps.Packaging; Convert Doc to XPS
The SaveAs method of Document class available in OfficeInterop allows us to save a word document as an XPS document. However, you must make sure you have version 12 of assembly added to your project as I mentioned before. The ConvertWordDocToXPSDoc method takes a full path of a word document file and new full path of XPS document and converts doc file to an xps file.
private XpsDocument ConvertWordDocToXPSDoc(string wordDocName, string xpsDocName) { // Create a WordApplication and add Document to it Microsoft.Office.Interop.Word.Application wordApplication = new Microsoft.Office.Interop.Word.Application(); wordApplication.Documents.Add(wordDocName); Document doc = wordApplication.ActiveDocument; // You must make sure you have Microsoft.Office.Interop.Word.Dll version 12. // Version 11 or previous versions do not have WdSaveFormat.wdFormatXPS option try { doc.SaveAs(xpsDocName, WdSaveFormat.wdFormatXPS); wordApplication.Quit(); XpsDocument xpsDoc = new XpsDocument(xpsDocName, System.IO.FileAccess.Read); return xpsDoc; } catch (Exception exp) { string str = exp.Message; } return null; }
Ellipse Control in WPF The Ellipse object represents an ellipse shape and draws an ellipse with the given height and width. The Width and Height properties of the Ellipse class represent the width and height of an ellipse. The Fill property fills the interior of an ellipse. The Stroke property sets the color and Stroke Thickness represents the width of the outer line of an ellipse. Creating an Ellipse The Ellipse element in XAML creates an ellipse shape. The following code snippet creates an ellipse by setting its width and height properties to 200 and 100 respectively. The code also sets the black stroke of width 4.
<Ellipse
Width="200"
Height="100"
Fill="Blue"
Stroke="Black"
StrokeThickness="4" />
Output-
Content Control in WPF A ContentControl has a limited default style. If you want to enhance the appearance of the control, you can create a new DataTemplate. Another typical scenario is to use the ContentControl to show more information about an item selected in an ItemsControl control. Content Model: ContentControl is the class from which other content controls inherit. XAML Code
Frame Control in WPF The Frame control in WPF supports content navigation within content. A Frame can be hosted within a Window, NavigationWindow, Page, UserControl, or a FlowDocument control.
XAML code -
<Window x:Class="WpfApplication5.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Window1" Height="300" Width="300"> <Grid Width="224"> <TextBlock>Outside area of frame</TextBlock>
<Frame Source="Page1.xaml">
</Frame> </Grid> </Window>
The Window looks like as below in picture. The Blue area is the Page1.xaml and white area is outside of the Frame.
Now you can manage contents of a frame the way you want. For example, the following code rotates the contents of frame to 45 angle.
<Frame Source="Page1.xaml">
<Frame.LayoutTransform>
<RotateTransform Angle="45" />
</Frame.LayoutTransform>
</Frame>
Output -
GroupBox Control in WPF The GroupBox element in XAML represents a GroupBox control. XAML Code for GroupBox Control
Grid Control In WPF Window Based The grid is a layout panel that arranges its child controls in a tabular structure of rows and columns. Its functionality is similar to the HTML table but more flexible. A grid consists of rows and columns . To setup this, use the following definition:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" <Grid.RowDefinitions> <RowDefinition Height="Auto" /> <RowDefinition Height="Auto" /> <RowDefinition Height="Auto" /> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition Width="75" /> <ColumnDefinition Width="Auto" /> </Grid.ColumnDefinitions>
A Grid has RowDefinitions and ColumnDefinitions that specify the height (specific to the row), the width (specific to the column), and possibly other dimensional aspects. Normally, you would expect to see a combination of row/cell or row/column objects to lay out the grid; however, WPF works differently. Each control has a Grid.Row and Grid.Column, which specifies the row/column to render in. The resize behaviour of the controls is defined by the HorizontalAlignment and VerticalAlignment properties who define the anchors. The distance between the anchor and the grid line is specified by the margin of the control.. Define Rows and Columns in Grid:
The grid has one row and column by default. To create additional rows and columns, you have to add RowDefinition items to the RowDefinitions collection and ColumnDefinition items to the ColumnDefinitions collection. The following example shows a grid with three rows and two columns.
The size can be specified as an absolute amount of logical units, as a percentage value or automatically. Fixed Fixed size of logical units (1/96 inch) Auto Takes as much space as needed by the contained control Star (*) Takes as much space as available, percentally divided over all star-sized columns. Star-sizes are like percentages, except that the sum of all star columns does not have to be 100%. Remember that star-sizing does not work if the grid size is calculated based on its content. How to Add Control To Grid: To add controls to the grid layout panel just put the declaration between the opening and closing tags of the Grid. Keep in mind that the row- and columndefinitions must precced any definition of child controls.
The grid layout panel provides the two attached properties Grid.Column and Grid.Row to define the location of the control..
Progressbar Control Using WPF Window Based The ProgressBar tag in XAML represents a WPF ProgressBar control. <ProgressBar></ProgressBar> The Width and Height properties represent the width and the height of a ProgressBar. The Name property represents the name of the control, which is a unique identifier of a control. The Margin property tells the location of a ProgressBar on the parent control. The HorizontalAlignment and VerticalAlignment properties are used to set horizontal and vertical alignments. Syntax of ProgressBar: <ProgressBar Margin="10,10,0,13" Name="ListView1" HorizontalAlignment="Left" VerticalAlignment="Top" Width="300" Height="30" /> Setting up ProgressBar Value : The Value property of ProgressBar sets up the current value of a ProgressBar control. In the following code, I set the Value property to 60 .
Next up, we are going to add an animation that will automatically fill and unfill the progress bar. So we are going to add a Trigger that fires after our progress bar has loaded. That trigger will fire a storyboard with an animation. The animation will be set to increase the ProgressBar.Value from 0 to 100 over the course of 1 second. We set the autoreverse property to true, to allow the animation to go forwards and backwards. The xaml to do this would be:
Menu Control Using WPF Window Based A Menu is a collection of menu items with a command associated with each menu items. A menu item may have children menu items called submenus. This article discusses how to work with menus in XAML and WPF applications. The Menu tag in XAML creates a menu control.
The Name property defines the name of the menu and Height and Width represents the height and width of a menu control. Syntax Of Menu Control: <Menu Margin="37,15,63,0" Name="menu1" Height="22" VerticalAlignment="Top" /> This Tag Creates a Menu Control. Important Properties: Name --> Defines the name of the menu. Height --> Defines the Height of the menu Width --> Defines the width of the menu Margin --> Defines the position of the menu Background--> Defines backcolor of the menu HorizontalAlignment --> Defines the horizontal alignment of the menu inside the parent control. HorizontalContentAlignment --> Defines the horizontal alignment for the menu content. VerticalAlignment --> Defines the vertical alignment of the menu inside the parent control. VerticalContentAlignment --> Defines the content alignment for the menu content. ToolTip --> defines the tooltip for the menu.
A MenuItem can have other MenuItem tags within it as child/sub menus and can go up to several levels. The following code adds three children menu items to first menu item.
The MenuItem.ToolTip tag adds a tooltip to a menu item. The following code adds a tooltip to the Open menu item. <MenuItem Header="_Open" IsCheckable="true">
<MenuItem.ToolTip>
<ToolTip>
Open a file.
</ToolTip>
</MenuItem.ToolTip>
</MenuItem>
Adding an Event Trigger to a MenuItem:
The Click event is used to add the menu item click event handler. The following code adds a click event handler for a menu item.
<MenuItem IsCheckable="true" Header="_Open" Click="MenuItem_Click"> The event handler is defined like following in the code behind. I added a message box when the menu item is clicked.
ListView Control Using WPF Window Based The ListView tag represents a WPF ListView control in XAML. <ListView></ListView> The Width and Height properties represent the width and the height of a ListView. The Name property represents the name of the control, which is a unique identifier of a control. The Margin property tells the location of a ListView on the parent control. The HorizontalAlignment and VerticalAlignment properties are used to set horizontal and vertical alignments. The following code snippet sets the name, height, and width of a ListView control. The code also sets horizontal alignment to left and vertical alignment to top.
Adding Item in ListView Dynamically Using WPF Window Based private void button1_Click(object sender, RoutedEventArgs e)
{
ListView1.Items.Add(textBox1.Text);
} On button click event handler, we add the content of TextBox to the ListView by calling ListView.Items.Add method. Now if you enter text in the TextBox and click Add Item button, it will add contents of the TextBox to the ListView. Example:
Deleting ListView Items:
The button click event handler looks like following. On this button click, we find the index of the selected item and call ListView.Items.RemoveAt method as following.
RadioButton Control Using WPF Window Based RadioButton controls are usually grouped together to offer user a single choice among Different options (only one button at a time can be selected). <RadioButton> </RadioButton> tag is used to create the radio button in XAML File.
Syntax of RadioButton: <RadioButton Height="18" Margin="92,58,0,0" Name="radioButton1" VerticalAlignment="Top" HorizontalAlignment="Left" Width="82">RadioButton</RadioButton> In the following tag I create the RadioButton in which Height is height of the RadioButton, Name is the name of the RadioButton, text in the between the RadioButton tag is the content visible to user. The Background and Foreground properties represent the background and foreground colors of a RadioButton. The following code sets the name, height, and width of a RadioButton control. The code also sets horizontal alignment to left and vertical alignment to top
Tab Control Using WPF Window Based Each TabControl can contain Multiple collection of TabItem elements. TabItem has two specific attributes. Header is the string value that you see on top of each tab and IsSelected is a Boolean value that specifies if a tab is selected. Bassically only one tab can be selected at a time otherwise the first tab in list will be selected. Two elements play main roles in Creating a tab control: TabControl and TabItem. TabControl is the container of one or more TabItem elements. Syntax of TabControl: <TabControl Margin="41,46,0,0" Name="tabControl1" Height="100" VerticalAlignment="Top" HorizontalAlignment="Left" Width="200" /> In the following tag I create the TabControl in which Height is height of the TabControl , Name is the name of the TabControl , The Background and Foreground properties represent the background and foreground colors of a TabControl.
The following code sets the name, height, and width of a RadioButton control. The code also sets horizontal alignment to left and vertical alignment to top <TabControl> <TabItem Header="Tab 1">Home</TabItem> <TabItem Header="Tab 2">Services</TabItem> <TabItem Header="Tab 3">Help</TabItem> <TabItem Header="Tab 4">Contact Us</TabItem> </TabControl> Example
</Window> Password Control in WPF Window Based The password box control is a special type of TextBox designed to enter passwords. The typed in characters are replaced by asterisks. Since the password box contains a sensible password it does not allow cut, copy, undo and redo commands. The PasswordBox control allows you to hide the characters and limit the number of characters to be typed in the editable area. Properties:
Password property contains the password in the PasswordBox and PasswordChar is the masking character for the password.
Following tag create the Password Box control with the masking character as * and maximum password length of 50 characters. The MaxLength property is used to get and set the maximum number of characters you can enter in a PasswordBox.
Event: To handle the event add the PasswordChanged="Password1_OnPasswordChanged" in the xaml and write the handler as PasswordChanged event is main event of the control and is raised when password property has been changed
</Window> ScrollViewer Control In WPF Window Based WPF provides a ScrollViewer control that is a container for other controls and creates a scrollable area. It is simple to create and add to a XAML file. The developer can choose to display a horizontal and/or vertical scroll bar. For this article, I chose to hide the scoll bar so that the user must scroll by clicking on the content in the ScrollViewer..In WPF scrolling or you can say the ScrollViewer is used when we want to fit the large amounts of content in a limited amount of space. The ScrollViewer control provides a convenient way to enable scrolling of content in WPF. ScrollViewer encapsulated the ScrollBars within it and displays it whenever it is required. As the ScrollViewer implements IScrollInfo is the main scrolling area inside the scrollviewer. ScrollViewer also responds to mouse and keyboard commands. Syntax of ScrollViewer:
</Grid> </Window> Slider Control in WPF Window Based Slider can be used as a selector for Diffrent values. These values (which are double) can have a minimum and maximum. You can put different tick values for these values to split this interval. On the other hand you can set some values for intervals and delays between movements and clicks on ticks. Important Properties : AutoToolTipPlacement: Specifies if a ToolTip must be shown and where it will be displayed. Delay: A value in milliseconds that specifies how long RepeatButton should wait before an decrease or increase. Interval: a value in milliseconds that specifies the time for waiting between repeats. LargeChange: The amount that should be added or subtracted to or from Value attribute when user clicks on scrollbar. Maximum: Maximum value for slider. Minimum: Minimum value for slider. Orientation: Gets to values, Horizontal or Vertical and specifies the orientation of slider Control. SmallChange: The amount that should be added or subtracted to or from Value attribute when user clicks on thumb. TickPlacement: Determines the place that ticks should be placed. Ticks: A required attribute and a set of double values for ticks. Value: ByDefault selected value for tick. Syntax Of Slider Control:
<Slider Name="Slider1" Width="200" Height="20"
Background="Gray" Maximum="80" Minimum="0">
</Slider>
In Above Code The Width and Height property represent the width and the height of the control. The Name property represents name of the control, which is a unique identifier of the control. The Background property is used to set the background color of the control. The Minimum and Maximum properties represent the minimum and maximum values of the slider range. Example: Original Image:
ViewBox control in WPF Window Based The Viewbox element automatically scales its content to fill the space available.the Viewbox control, which expands a control to fill the available space (while keeping the aspect ratio) but the control can assume absolute size. The view box could only have one child; else if a view box contains more than one child, an argument exception will be thrown. Viewbox and Viewport are the properties of ImageBursh, DrawingBrush, TileBrush and VisualBrush elements in Windows Presentation Foundation. With these two attributes we can crop images and shapes. Example of ViewBox Control In WPF Real Image:
Figure 2: See the image after selecting an area in ViewBox Control.
<!-- Here By passing the four value in ViewBox I will select a area from Real Image-->
<ImageBrush ImageSource="D:\Documents and Settings\All Users\Documents\My Pictures \Sample Pictures\Sunset.jpg" Viewbox="0.2,0.3,0.1,0.5" />
</Rectangle.Fill>
</Rectangle>
</Grid> </Window>
After set the property Stretch="Uniform" of image we will get:
Separator Control in WPF Window Based This article explains how to draw a separator in a GroupBox. The separator separates the OK and Cancel buttons from the rest of the content in that GroupBox. This is a UI design seen relatively often in WPF Windows Application.. Example:
<Window x:Class="WpfApplication7.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Window1" Height="370" Width="417"> <Grid Height="260" Width="302"> <GroupBox Name="settingsGroupBox" Header="Select Course" Margin="4,4,32,99" Background="Azure"> <StackPanel Height="141" Width="259" Background="Honeydew"> <StackPanel.Resources> <!-- Give the CheckBoxes some room to breath. --> <Style TargetType="CheckBox"> <Setter Property="Margin" Value="4" /> </Style> </StackPanel.Resources>
<!-- Some CheckBoxes that represent settings. --> <CheckBox Name="chk1" > .Net FrameWork With C# </CheckBox > <CheckBox Name="chk2"> java And Advanced Java </CheckBox> <CheckBox Name="chk3"> PHP,CAKE PHP,Smarty </CheckBox> <CheckBox Name="chk4" > Oracle,Sql Server </CheckBox>
<!-- A separator between settings and buttons. --> <Line Margin="0,4" SnapsToDevicePixels="True" Stroke="{Binding ElementName=settingsGroupBox, Path=BorderBrush}" Stretch="Fill" X1="0" X2="1" />
<!-- The standard OK and Cancel Buttons. --> <StackPanel HorizontalAlignment="Right" Orientation="Horizontal" > <StackPanel.Resources> <Style TargetType="Button"> <Setter Property="Margin" Value="4" /> <Setter Property="Width" Value="60" /> </Style> </StackPanel.Resources> <Button Click="Button_Click">_OK</Button> <Button Click="Button_Click_1">_Cancel</Button> </StackPanel> </StackPanel> </GroupBox>
TreeView Control in WPF Window Based A TreeView Shows data in a hierarchical Form in a parent child relationship where a parent node can be expanded .You can use the TreeView control to display information from a wide variety of data sources such as an XML file, site-map file, string, or from a database. or you can say TreeView control is a hierarchical structure to display the data. its look like a tree. it also contain root node, parent node and child node like tree. Features: Internet Explorer Favorites style view. Append a number to any node, allowing you to create an Outlook style folder list with message counts. Choose Font and Color on an item-by-item basis, and set the control's back color. Bolding of items Info Tip (multi-line tooltip) support with full control over Info Tip color Works with Microsoft or API ImageList. Item check boxes Full-Row select mode Single-Click expand mode Supports Two Images per item: one standard icon and a state icon. Disable Custom Draw for maximum speed Speed-optimized Clear method Example of TreeView Control:
foreach (string item in items[tv.Header.ToString()])
tv.Items.Add(item);
} }
Designed Output-
UniformGrid Control in WPF Window Based The UniformGrid Control arranges content in its area so that all the cells in the grid have the same dimension. It represents a perfect solution if someone prefers to prevent the headache of ordering or arrange controls within an ordinary Grid. First, the Uniform grid belongs to the System.Windows.Controls. Primitives. It could be found in the tool box at the bottom. A UniformGrid show child elements or control in columns and rows of the same size. Or we can say The number of cells is adjusted to accommodate the number of controls. Example: XAML Code-
WrapPanel Control in WPF Window Based Wrap panel wraps all child elements to new lines if no space is left. The wrap panel is similar to the StackPanel but it does not just stack all child elements to one row, it wraps them to new lines if no space is left. The Orientation can be set to Horizontal or Vertical.The WrapPanel can be used to arrange tabs of a tab control, menu items in a toolbar or items in an Windows Explorer like list. The WrapPanel is often used with items of the same size, but its not a requirement. Example:
</WrapPanel> </Grid> </Window> Image Control in WPF Window The Image control provides rich features to display images of various formats like JPEG, PNG, ICO, BMP, GIF etc. Displaying an image is as simple as setting the Image. Source property to the appropriate image file path. No special coding is required to work with different file formats.
Rectangle Control in WPF Window Based Rectangle Means From Latin: rectus "right" + angle A 4-sided polygon where all interior angles are 90 The rectangle, like the square, is one of the most commonly known quadrilaterals. It is defined as having all four interior angles 90 (right angles). The Rectangle object represents a rectangle shape and draws a rectangle with the given height and width. The <Rectangle /> element of XAML draws a rectangle. The Height and Width attributes represent the height and width of the rectangle. The Stroke and StrokeThickness represents the color and thickness of the rectangle boundary. Example: XAML Code-
in This code The Fill attributes fill a rectangle with a color. The following code fills a rectangle with pink color. By setting RadiusX and RadiusY attributes, you can also draw a rectangle with rounded corders. Output: