0% found this document useful (0 votes)
100 views

Visual Programming: Lecture # 13

This document provides an overview of visual programming in WPF using XAML. It discusses key concepts like WPF, XAML, and GUI frameworks. It then demonstrates how to create basic WPF applications and controls using XAML markup, including labels, text boxes, buttons, images and panels like grids, canvases and stacks. It also covers more advanced topics like data binding, scrolling, and navigation between pages.

Uploaded by

Umer Ali
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
100 views

Visual Programming: Lecture # 13

This document provides an overview of visual programming in WPF using XAML. It discusses key concepts like WPF, XAML, and GUI frameworks. It then demonstrates how to create basic WPF applications and controls using XAML markup, including labels, text boxes, buttons, images and panels like grids, canvases and stacks. It also covers more advanced topics like data binding, scrolling, and navigation between pages.

Uploaded by

Umer Ali
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 119

VISUAL PROGRAMMING

LECTURE # 13
KEY CONCEPTS …

WPF
XAML
WHAT IS WPF?
WORKING

 WPF, which stands for Windows Presentation Foundation, is Microsoft's latest


approach to a GUI framework, used with the .NET framework.
 But what IS a GUI framework? GUI stands for Graphical User Interface, and
you're probably looking at one right now. Windows has a GUI for working with
your computer, and the browser that you're likely reading this document in has
a GUI that allows you to surf the web.
 Here are a lot of GUI frameworks out there, but for .NET developers, the most
interesting ones are currently WinForms and WPF. WPF is the newest, but
Microsoft is still maintaining and supporting WinForms. As you will see there
are quite a few differences between the two frameworks, but their purpose is
the same: To make it easy to create applications with a great GUI.
 XAML, which stands for eXtensible Application Markup Language, is Microsoft's variant of XML for
describing a GUI.
 In previous GUI frameworks, like WinForms, a GUI was created in the same language that you would use
for interacting with the GUI, e.g. C# or VB.NET and usually maintained by the designer (e.g. Visual
Studio), but with XAML, Microsoft is going another way. Much like with HTML, you are able to easily
write and edit your GUI.
Your text here
 Whether you're creating a Window or a Page, it will consist of a XAML document and a CodeBehind file,
which together creates the Window/Page. The XAML file describes the interface with all its elements,
while the CodeBehind handles all the events and has access to manipulate with the XAML controls.
 Creating a control in XAML is as easy as writing it's name, surrounded by angle brackets. For instance, a
Button looks like this:

XAML
CREATE WPF
APPLICATION
APP.XAML
WORKING
WORKING
LABEL
LABEL
TEXTBOX
BUTTON
CHECKBOX
RADIO BUTTON
PASSWORD BOX
IMAGE CONTROL
WPF PANELS
CANVAS CONTROL
<Canvas>
<Ellipse Panel.ZIndex="2" Fill="Gainsboro" Canvas.Left="25" Canvas.Top="25" Width="200" Height="200" />
<Rectangle Panel.ZIndex="3" Fill="LightBlue" Canvas.Left="25" Canvas.Top="25" Width="50" Height="50" />
<Rectangle Panel.ZIndex="2" Fill="LightCoral" Canvas.Left="50" Canvas.Top="50" Width="50" Height="50" />
<Rectangle Panel.ZIndex="4" Fill="LightCyan" Canvas.Left="75" Canvas.Top="75" Width="50" Height="50" />
</Canvas>
WRAP PANEL
STACK PANEL
DOCK PANEL
GRID
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Button VerticalAlignment="Top" HorizontalAlignment="Center">Button 1</Button>
<Button Grid.Column="1" VerticalAlignment="Center" HorizontalAlignment="Right">Button 2</Button>
</Grid>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Button Grid.ColumnSpan="2">Button 1</Button>
<Button Grid.Column="3">Button 2</Button>
<Button Grid.Row="1">Button 3</Button>
<Button Grid.Column="1" Grid.Row="1" Grid.RowSpan="2" Grid.ColumnSpan="2">Button 4</Button>
<Button Grid.Column="0" Grid.Row="2">Button 5</Button>
</Grid>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="5" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<TextBlock FontSize="55" HorizontalAlignment="Center" VerticalAlignment="Center"
TextWrapping="Wrap">Left side</TextBlock>
<GridSplitter Grid.Column="1" Width="5" HorizontalAlignment="Stretch" />
<TextBlock Grid.Column="2" FontSize="55" HorizontalAlignment="Center" VerticalAlignment="Center"
TextWrapping="Wrap">Right side</TextBlock>
</Grid>
LISTVIEW CONTROL
SCROLL VIEWER
 There are two predefined elements that enable scrolling in WPF applications: ScrollBar and 
ScrollViewer. The ScrollViewer control encapsulates horizontal and vertical ScrollBar elements
and a content container (such as a Panel element) in order to display other visible elements in a
scrollable area. 
 The ScrollViewer control responds to both mouse and keyboard commands

<ScrollViewer HorizontalScrollBarVisibility="Auto">
<StackPanel VerticalAlignment="Top" HorizontalAlignment="Left">
<TextBlock TextWrapping="Wrap" Margin="0,0,0,20">
Scrolling is enabled when it is necessary. Resize the window, making it larger and smaller.
</TextBlock>
<Rectangle Fill="Red" Width="500" Height="500"></Rectangle>
</StackPanel>
</ScrollViewer>

 By setting it to Auto, the scrollbars will be invisible until the content actually goes beyond the
available space, which is usually what you want.
FLOW DOCUMENT SCROLL VIEWER

  It simply allows the users to scroll to long documents, using regular scrollbars. Since this is our first
meeting with the FlowDocument used in any form, we'll start off with a basic "Hello World!" example,
and besides the use of FlowDocumentScrollViewer, this article will also cover several concepts common
between all of the wrappers.

<Grid>
<FlowDocumentScrollViewer>
<FlowDocument>
<Paragraph FontSize="36">Hello, world!</Paragraph>
<Paragraph FontStyle="Italic" TextAlignment="Left" FontSize="14" Foreground="Gray">
The ultimate programming greeting!
</Paragraph>
</FlowDocument>
</FlowDocumentScrollViewer>
</Grid>
DATA BINDING
DESCRIPTION

 Data binding is general technique that binds two data/information sources together and maintains
synchronization of data.
 Data binding in WPF is the preferred way to bring data from your code to the UI layer. Sure, you can
set properties on a control manually or you can populate a ListBox by adding items to it from a loop,
but the cleanest and purest WPF way is to add a binding between the source and the destination UI
element.
<StackPanel Margin="15">
<WrapPanel>
<TextBlock Text="Window title: " />
<TextBox Name="txtWindowTitle" Text="{Binding Title, UpdateSourceTrigger=Explicit}" Width="150"
/>
<Button Name="btnUpdateSource" Click="btnUpdateSource_Click" Margin="5,0"
Padding="5,0">*</Button>
</WrapPanel>
<WrapPanel Margin="0,10,0,0">
<TextBlock Text="Window dimensions: " />
<TextBox Text="{Binding Width, UpdateSourceTrigger=LostFocus}" Width="50" />
<TextBlock Text=" x " />
<TextBox Text="{Binding Height, UpdateSourceTrigger=PropertyChanged}" Width="50" />
</WrapPanel>
</StackPanel>
PAGE NAVIGATION
MAIN WINDOW CODE
<StackPanel Orientation="Horizontal" VerticalAlignment="Top">
<Button Content="Page 1" Height="40" MinWidth="100" Click="btnClick1"/>
<Button Content="Page 2" Height="40" MinWidth="100" Margin="10,0,0,0" Click="btnClick2"/>
</StackPanel>

<Frame x:Name="_mainFrame" />

C# CODE
private void btnClick1(object sender, RoutedEventArgs e)
{
_mainFrame.Content = new Page1();
}

private void btnClick2(object sender, RoutedEventArgs e)


{
_mainFrame.Content = new Page2();
}
THANK YOU

You might also like