Visual Programming: Lecture # 13
Visual Programming: Lecture # 13
LECTURE # 13
KEY CONCEPTS …
WPF
XAML
WHAT IS WPF?
WORKING
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>
C# CODE
private void btnClick1(object sender, RoutedEventArgs e)
{
_mainFrame.Content = new Page1();
}