List of the third game series in WPF

Source: Internet
Author: User

This article describes how to automatically generate a game interface through C #. It mainly demonstrates how to display the list of all items after clicking "My Shop. The Data Source comes from Access 2007. Of course, System. Data. OleDb will be used.

1. Add the MouseLeftButtonDown event to "My Shop" Image.

<Image Source="image/market.png" Width="125" Height="125" Cursor="Hand"
Tag="My Shop" Canvas.Left="150" Canvas.Top="13" MouseEnter="Image_BlurEffect_MouseEnter"
MouseLeave="Image_BlurEffect_MouseLeave" MouseLeftButtonDown="Image_MouseLeftButtonDown" ></Image>

2. Create a Data query function for later use (use System. Data. OleDb ). I also want to raise a question here. Is there a better way to query and return query data?

// Sqlc is used to calculate the number of data rows. SQL is used to query and returns an object array private object [,] Data_Query (string sqlc, string SQL) {// The Access 2007 Provider must use ACE. OLEDB.12.0
OleDbConnection aConnection =
New OleDbConnection ("Provider = Microsoft. ACE. OLEDB.12.0; Data Source = xmarket. accdb "); OleDbCommand aCommand = new OleDbCommand (); aConnection. open (); aCommand. connection = aConnection; // calculates the number of rows aCommand. commandText = sqlc; OleDbDataReader countReader = aCommand. executeReader (); countReader. read (); int num = countReader. getInt32 (0); countReader. close (); // query data aCommand. commandText = SQL; OleDbDataReader queryReader = aCommand. executeReader (); // gets the number of queried columns int fieldNum = queryReader. fieldCount; // create the return array object [,] queryRes = new object [num, fieldNum]; int I = 0;
// Write data to the array while (queryReader. read () {for (int j = 0; j <fieldNum; j ++) {queryRes [I, j] = queryReader. getValue (j);} I ++;} queryReader. close (); aConnection. close (); return queryRes ;}

3. Now we need to write the Image_MouseLeftButtonDown event:

private void Image_MouseLeftButtonDown(object sender, MouseButtonEventArgs e){
… …
}

3.1. Since the original design is to display all interfaces in queryGrid (for queryGrid, see Layout Design in one of the WPF programming game series) components, when you click "My Home", there may be other interface content in queryGrid. Therefore, you must clear queryGrid first.

// Clear all rows and columns in queryGrid
QueryGrid. RowDefinitions. Clear (); queryGrid. ColumnDefinitions. Clear (); // Clear all child components in queryGrid
QueryGrid. Children. Clear (); // sets the interval between queryGrid and queryScrollViewer. queryGrid. Margin = new Thickness (20 );

3.2. Determine which icon is clicked

// Define sender as Image
Image image = sender as Image;
// Obtain Image Tagstring imageTag = image. Tag. ToString ();
// If (imageTag = "My Shop "){... ...}

 

3.2.1. Adjust the background color of queryScrollViewer and the border color of queryBorder, and set the number of rows and columns of queryGrid.

// I personally feel that the color of Brushes. SystemColor is limited, and the names of known colors cannot be understood,
// Use Color. FromRgb to set the Color.
QueryScrollViewer. background = new SolidColorBrush (Color. fromRgb (255,204,102); queryBorder. borderBrush = new SolidColorBrush (Color. fromRgb (255,102, 51); // SQLstring sqlc = "select count (*) from goods where available = 1 and typeid = 1 "; string SQL = "select * from goods where available = 1 and typeid = 1 ";
// Query the object [,] res = Data_Query (sqlc, SQL );
// Query the number of items int num = res. Length/7;
// Because five items need to be placed in each row, you can redefine the number of rows int rows = Convert. toInt32 (Math. ceiling (Convert. toDouble (num)/5); // Add a rows row for queryGrid
For (int ri = 0; ri <rows; ri ++) {RowDefinition rd = new RowDefinition (); // you can specify the Row Height.
Rd. Height = new GridLength (210); queryGrid. RowDefinitions. Add (rd);} // Add 5 columns to queryGrid
For (int ci = 0; ci <5; ci ++) {ColumnDefinition cd = new ColumnDefinition (); queryGrid. ColumnDefinitions. Add (cd );}

3.2.2. If you already know that there are num items in 3.2.1, add them to queryGrid.

// RowNum and colNum are used to define the row and column position of an item in queryGrid.
Int rowNum = 0;

Int colNum = 0;
// Create an item unit for (inti = 0; I <num; I ++) {// each item has a Border BordergoodsBorder = newBorder (); // define the Border style goodsBorder. background = newSolidColorBrush (Color. fromRgb (255,255,204); goodsBorder. borderBrush = newSolidColorBrush (Color. fromRgb (255,102, 51); goodsBorder. borderThickness = newThickness (3); goodsBorder. cornerRadius = newCornerRadius (5); goodsBorder. width = 150; goodsBorder. height = 195; // set the Border position. Five items in each row if (colNum = 5) {rowNum ++; colNum = 0 ;} // set the row and column attributes for the Border goodsBorder. setValue (Grid. rowProperty, rowNum); goodsBorder. setValue (Grid. columnProperty, colNum); // Add Border to queryGrid. children. add (goodsBorder); colNum ++; // create a StackPanel because the item information needs to be displayed vertically.
StackPanel goodsStackPanel = new StackPanel ();
// Set the StackPanel display mode goodsStackPanel. Orientation = Orientation. Vertical;
GoodsStackPanel. HorizontalAlignment = HorizontalAlignment. Center;
GoodsStackPanel. Margin = new Thickness (5 );
// Add StackPanel to Border goodsBorder. Child = goodsStackPanel;


// The first item Image to be displayed, which defines the Image
Image goodsImage = new Image (); // create an Image Source
BitmapImage bitImage = new BitmapImage ();
BitImage. BeginInit ();
// Add the image name of the database to UriSource
BitImage. UriSource = newUri ("image/shop/" + res [I, 2]. ToString (), UriKind. Relative );
BitImage. EndInit ();
GoodsImage. Source = bitImage;
GoodsImage. Height = 80;
GoodsImage. Width = 80;
GoodsImage. Margin = new Thickness (5 );
// Add the Image to StackPanel
GoodsStackPanel. Children. Add (goodsImage); // display the Textblock of the item price
TextBlock goodsPrice = new TextBlock ();
GoodsPrice. Text = "Price: $" + res [I, 6]. ToString ();
GoodsPrice. Margin = new Thickness (5 );
// Add the number of items to StackPanel
GoodsStackPanel. Children. Add (goodsPrice); // display the number of items Textblock TextBlock goodsQty = new TextBlock ();
GoodsQty. Text = "Quantity:" + res [I, 5]. ToString (); // register the Name in queryGrid because you need to modify the Quantity value later.
GoodsQty. Name = "gQty" + res [I, 0]. ToString (); // determine whether the Name has been registered before registration

ObjectfindTextObj = queryGrid. FindName ("gQty" + res [I, 0]. ToString ());
If (findTextObj! = Null)
{// If you have registered queryGrid. UnregisterName ("gQty" + res [I, 0]. ToString ());
}
QueryGrid. RegisterName (goodsQty. Name, goodsQty );
GoodsQty. Margin = new Thickness (5 );
// Add the number of items to queryGrid
GoodsStackPanel. Children. Add (goodsQty );
}

 

3.3. Display queryGrid

queryCanvas.Visibility = Visibility.Visible;

4. Goods table structure

5. Next, please make more bricks.

To be continued ......

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.