Yes, this is still a listbox!

Similar documents
Weather forecast ( part 2 )

CPSC 481 Tutorial 10 Expression Blend. Brennan Jones (based on tutorials by Bon Adriel Aseniero and David Ledo)

CPSC Tutorial 9 Blend & Animations

My own Silverlight textbox


CPSC Tutorial 6

CS3240 Human-Computer Interaction Lab Sheet Lab Session 2

XAML - BUTTON. The Button class represents the most basic type of button control. The hierarchical inheritance of Button class is as follows

CS3240 Human-Computer Interaction Lab Sheet Lab Session 3 Designer & Developer Collaboration

Introduction to Data Templates and Value Converters in Silverlight

Windows Presentation Foundation Programming Using C#

Master Code on Innovation and Inclusion

sharpcorner.com/uploadfile/37db1d/4958/default.aspx?articleid=cb0b291c-52ae-4b80-a95c- 438d76fa1145

Programming in C# Project 1:

Hands-On Lab. Hello Windows Phone

Create your own Meme Maker in C#

Tutorials. Lesson 3 Work with Text

Windows Presentation Foundation. Jim Fawcett CSE687 Object Oriented Design Spring 2018

Building Next Generation GUIs using Microsoft Expression Interactive Designer

Microsoft Exam

CHAPTER 1: INTRODUCING C# 3

Name of Experiment: Student Database

Windows Presentation Foundation (WPF)

WebAqua.NET 2.0 White Paper

Microsoft Exam TS: Windows Applications Development with Microsoft.NET Framework 4 Version: 58.5 [ Total Questions: 228 ]

NE Fundamentals of XAML and Microsoft Expression Blend

1) ItemsSource: it point to collection which has all the objects that will be shown in the list

Tutorial 3 - Welcome Application

Week 6: First XAML Control Exercise

CS3240 Human-Computer Interaction

This tutorial is designed for software developers who want to learn how to develop quality applications with clean structure of code.

WPF AND SILVERLIGHT RESOURCES

Getting Started with Banjos4Hire

[MS10553]: Fundamentals of XAML and Microsoft Expression Blend

Click on the empty form and apply the following options to the properties Windows.

We are going to use some graphics and found a nice little batman running GIF, off course you can use any image you want for the project.

Step4: Now, Drag and drop the Textbox, Button and Text block from the Toolbox.

Beginning Silverlight 5 in C #

Fundamentals of XAML and Microsoft Expression Blend

Class Test 5. Create a simple paint program that conforms to the following requirements.

Week 8: Data Binding Exercise (Bookstore)

Paint/Draw Tools. Foreground color. Free-form select. Select. Eraser/Color Eraser. Fill Color. Color Picker. Magnify. Pencil. Brush.

Custom Events Overridable Methods... 7

Accurate study guides, High passing rate! IT TEST BOOK QUESTION & ANSWER. Ittestbook provides update free of charge in one year!

EXAM TS:Windows Apps Dev w/microsoft.net Framework 4((C# and VB) Buy Full Product.

Exam sell. Higher Quality Better Service! Certified IT practice exam authority.

Adding CSS to your HTML

Lab - 1. Solution : 1. // Building a Simple Console Application. class HelloCsharp. static void Main() System.Console.WriteLine ("Hello from C#.

Form Properties Window

Visual C# Program: Simple Game 3

WRITING THE MANAGEMENT SYSTEM APPLICATION

ArcGIS Pro SDK for.net: Advanced User Interfaces in Add-ins. Wolfgang Kaiser

C# is intended to be a simple, modern, general-purpose, objectoriented programming language. Its development team is led by Anders Hejlsberg.

CSIS 1624 CLASS TEST 6

C# 2008 and.net Programming for Electronic Engineers - Elektor - ISBN

EXCEL + POWERPOINT. Analyzing, Visualizing, and Presenting Data-Rich Insights to Any Audience KNACK TRAINING

Programming Windows, Sixth Edition

Microsoft CSharp

The Open Core Interface SDK has to be installed on your development computer. The SDK can be downloaded at:

CS3240 Human-Computer Interaction Lab Sheet Lab Session 4 Media, Ink, & Deep Zoom

Now find the button component in the tool box. [if toolbox isn't present click VIEW on the top and click toolbox]

Applied WPF 4 in Context US $49.99 SOURCE CODE ONLINE

USER GUIDE MADCAP FLARE Tables

Christmas Card Final Image Preview Coach Christian s Example Ctrl+N 1920px 1200px RGB 72 pixels/inch Rectangle Tool (U)

Experiment 5 : Creating a Windows application to interface with 7-Segment LED display

Labels and Envelopes in Word 2013

Intermediate Microsoft Office 2016: Word

In-Class Worksheet #4

PART I: INTRODUCTION TO WINDOWS 8 APPLICATION DEVELOPMENT CHAPTER 1: A BRIEF HISTORY OF WINDOWS APPLICATION DEVELOPMENT 3

Graffiti Wallpaper Photoshop Tutorial

IBSDK Quick Start Tutorial for C# 2010

To start we will be using visual studio Start a new C# windows form application project and name it motivational quotes viewer

Creating Labels using Label Designer

A Step-by-step guide to creating a Professional PowerPoint Presentation

You can call the project anything you like I will be calling this one project slide show.

Index. Windows 10 running, 199 suspended state, 199 terminate apps,

Start Visual Studio, create a new project called Helicopter Game and press OK

10Tec igrid for.net 6.0 What's New in the Release

Start Visual Studio, start a new Windows Form project under the C# language, name the project BalloonPop MooICT and click OK.

create 2 new grid lines

Road Map for Essential Studio 2010 Volume 1

Report Designer Report Types Table Report Multi-Column Report Label Report Parameterized Report Cross-Tab Report Drill-Down Report Chart with Static

Lecture # 6 Engr. Ali Javed 11th March, 2014

Exam Name: TS: Windows Applications Development with Microsoft.NET Framework 4

REVIT ARCHITECTURE 2016

Microsoft Word Training

--Microsoft-- --Windows Phone--

Course 2D_SL: 2D-Computer Graphics with Silverlight Chapter C5: The Complete Code of PathAnimation. Copyright by V. Miszalok, last update:

Developing Windows Applications with Microsoft Visual Studio 2010

ComponentOne. HyperPanel for WPF

What s New Essential Studio User Interface Edition, 2011 Volume 4

END-TERM EXAMINATION

Nebraska - eforms. Tips and Tricks

WebFront for Service Manager

Visual.NET Extensions Tutorial

Dive Into Visual C# 2008 Express

Creating a Basic A/V presentation

Chapter 4: Single Table Form Lab

Week 5 Creating a Calendar. About Tables. Making a Calendar From a Table Template. Week 5 Word 2010

XNA 4.0 RPG Tutorials. Part 3. Even More Core Game Components

Transcription:

Yes, this is still a listbox! Step 1: create a new project I use the beta 2 of Visual Studio 2008 ( codename Orcas ) and Expression Blend 2.0 September preview for this tutorial. You can download the beta2 form http://msdn2.microsoft.com/en-us/vstudio/aa700831.aspx. The preview can be downloaded from http://www.microsoft.com/expression/products/features.aspx?key=blend2preview. Start Visual Studio and choose File New Project. Choose Visual C# as language and a WPF Application as template. Also fill in a name for the project and a location. Step 2: create the database The database is really simple. I got one table called Results who has 5 fields: Id, StudentName, Result, Comment and Picture. The database can be created with Access 2007, SQL Server, MySql, or any database engine you want. After creating the database, I added some data to test the project. Note: make sure the image exists on the path that you defined in the field Picture.

Step 3: create the class StudentResult We create a class that describes an object StudentResult. The class contains 5 properties: an id, the name of the student, the result of the student, some comments about the result and the path to an image. I have also 2 constructors added. Finally I override the ToString method with the name of the student Right click the project in the solution explorer and choose Add Class. Name the class StudentResult.cs and add the following code using System; using System.Collections.Generic; using System.Text; namespace MyAdvancedListbox public class StudentResult // Fields private string _id; private string _name; private int _result; private string _comment; private string _picture; // Properties public string Id get return _id; set _id = value; public string Name get return _name; set _name = value; public int Result get return _result; set _result = value; public string Comment get return _comment; set _comment = value; public string Picture get return _picture; set _picture = value; // Constructors public StudentResult() public StudentResult(string id, string name, int result, string comment, string picture)

this.id = Id; this.name = name; this.result = result; this.comment = comment; this.picture = picture; // Methods public override string ToString() return this.name; Step 4: create the class StudentResultDataAccess and use the Database class I use a class to communicate with the database. This is the file Database.cs. This class contains several methods to access the database. You can find it in the *.rar file with the entire project. Right click the project in the solution explorer and choose Add Existing item. Look for the file Database.cs and add it to the project. Right click the project in the solution explorer and choose Properties Under the settings section, add a new setting called ConnectionString of the type connectionstring and select your database. In the StudentResultDataAccess class we create a function which returns a list of StudentResult objects. In the same file I define a new class ResulList that inherits from an ObservableCollection. In the constructor of this class I call the function getstudentsresults and add every item form this list to the ObservableCollection. The code looks like this: using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Data; using System.Data.OleDb; using System.Text; namespace MyAdvancedListbox class StudentResultData public static List<StudentResult> getstudentresults() try List<StudentResult> resultlist = new List<StudentResult>(); string sql = "SELECT Id, StudentName, Result, Comment, Picture FROM Results ORDER BY Id"; DataTable dt = Database.GetDT(sql); foreach (DataRow dr in dt.rows) StudentResult sr = new StudentResult();

sr.id = Convert.ToString(dr["ID"]); sr.name = Convert.ToString(dr["StudentName"]); sr.result = Convert.ToInt32(dr["Result"]); sr.comment = Convert.ToString(dr["Comment"]); sr.picture = Convert.ToString(dr["Picture"]); resultlist.add(sr); return resultlist; catch (Exception ex) throw ex; public class ResultList: ObservableCollection<StudentResult> public ResultList() List<StudentResult> resultlist = StudentResultData.getStudentResults(); foreach(studentresult sr in resultlist) base.add(sr); Step 5: Add brush resources to the project Open your project with Expression Blend and do the following: Set the size of the window element to 800 by 600 Draw a listbox on the stage. The listbox fills the entire stage with a margin of 8px on each side. Set the background of the listbox to no brush and the border to the brush MyYellow. Set the border thickness to 8 on each side. Name the listbox lstresults Now we will add some resource brushes to the project, so we have a nice color scheme. The website http://kuler.adobe.com/ offers a nice tool to pick the correct colors.

Open your solution with expression blend Select the element Window in the objects and timeline panel Select the property background in the properties panel Insert the RGB values you want Press the button to convert the brush to a resource Choose a name for the brush and define it in the application so you can reuse it in different windows. Do this for all the colors you want. You can also do this manually in the app.xaml file: <Application.Resources> <SolidColorBrush x:key="myyellow" Color="#FFE6E2AF"/> <SolidColorBrush x:key="mygray" Color="#FFA7A37E"/> <SolidColorBrush x:key="mywhite" Color="#FFEFECCA"/> <SolidColorBrush x:key="myblue" Color="#FF046380"/> <SolidColorBrush x:key="mygreen" Color="#FF042F2F"/> </Application.Resources> After you added al the resource brushes, set the background for the window to MyWhite.

Step 6: bind the data to the listbox Go to the project tab. Under the data section, click the +CLR Object button. Select the item ResultList Right click the listbox on the stage and choose Bind Itemssource to data. Select the ResultList (Array) and click on the button Define Datatemplate Set the settings just like in the screenshot and click Ok. The data is now bound to the listbox, but the items look pretty ugly. We will change this in the next step.

Step 7: Define our own ControlTemplate In this step, we will create our own template for the listboxitems, so that they look just the way we want. Select the listbox in the objects and timeline panel Go to Object Edit other Templates Edit Generated Items ( ItemTemplate ) Edit Template Now, you can edit the template of the listboxitems. Select the Stackpanel and set the orientation to Horizontal and set the background to nothing Set the VerticalAlignment for all the items under the stackpanel to center Set the foreground of the textblock to MyGreen Make the stackpanel bigger and put the controls where you want them. Just like in the screenshot for example. Note: to align all the controls on the same line, give the controls a width in pixels instead of Auto. Make sure that the width of the stackpanel is set to auto. If we run the program, we already have a nice looking listbox. But if you select an item, you see an ugly blue border. We will change this to our blue border. Leave the template by clicking on [Listbox] at the top of the screen. Select the element listbox in the objects and timeline panel Go to Object Edit other styles Edit ItemContainerStyle Edit a copy Choose a name for the style and define it in the application. So you can reuse it in a other listbox Right click the element style in the objects and timeline panel and choose Edit Control Parts (Template) Edit template Reset the databinding on the backcolor property of the border by clicking the yellow square near the property and choose reset. Click on the isselected trigger Change the background property of the border to MyBlue Do the same for the trigger IsSelected AND IsSelectionActive Test the program, and you will see when an item is selected the background changes to MyBlue.

Step 8: Give visual feedback I also want to give visual feedback to my user that indicates if a result is good or not. We will add a progressbar to the listboxitem template and bind the value property to the result. After that we will break the template of the progressbar and use our colors. Open the template of the listboxitem Select a progressbar from the toolbox and add it to the template Change the foreground from green to MyGray Set the minimum to 0 and the maximum to 20 Click the gray square next to the value property Choose data binding Select the tab element property On the left side, select the textbox which contains the result On the right side, select the Text property Click Finish Right click the progressbar and choose Edit Control Parts (Template) Edit template Choose a name for the template and save it in the application ( reuse it on other progress bars ) Select the Part_Indicator which contains a grid foreground Open the grid foreground and change the backgroundcolor for the indication and animation rectangles. Test the application and you will see a progressbar in your own colors.

Step 9: Change the behavior of the listbox In this final step, we will change the behavior of the listbox in two ways: If I press the TAB key in the last textbox ( comment ) then the next listboxitem must be selected If I click in a textbox, this must become the selected item. We need to call different events on the textbox for this: Go back to the template of the listboxitem Select the stackpanel and search the Tag property Click the square near the Tag property and choose Data Binding Select the tabpage Explicit Data Context, choose the element ResultList and click finish. Select the textbox with the result Click the events button in the properties panel Choose the GotFocus event and give a name to it ( for example: Result_GotFocus) Visual Studio will open and you can insert this code: private void Result_GotFocus(object sender, RoutedEventArgs e) givefocus(sender); private void givefocus(object sender) TextBox txt = (TextBox)sender; StackPanel stp = (StackPanel)txt.Parent; lstresults.selecteditem = (StudentResult)stp.Tag; Do the same for the textbox with the comment. Go back to Expression Blend and select the keydown event of the textbox with the comment. Visual Studio will open again and you can insert this code: // if the TAB is pressed, select the next element if (e.key == System.Windows.Input.Key.Tab) // only if not the last element is selected if (lstresults.selectedindex + 1 < lstresults.items.count) lstresults.selectedindex += 1; else // else select the first element lstresults.selectedindex = 0;

Step 10: Detect changes in the data You can also detect changes in the results and write them back to your database. I describe this process in my tutorial on the ObservableCollection.