I am new to WPF, and I want to develop an application for Theater seating in WPF- c#.net. I did some research but i am lost as i do not know how each seat's data (sold or not, price, in which row) should be eventually bound to field in a table in SQL Express. Should i be using ObservableCollection to populate the contents of a Datatable in it? Also, should i be using a grid of buttons? or rectangles? How should i proceed 开发者_如何学Goif i want the front left and front right buttons to be inclined a bit instead of being horizontal? Should i be using a wrappanel if i want to have three areas for the seats? Left area, space for alter, middle area, space for alter and right area? If somebody can give me some hints for me to start, i will be more than glad. Thanks in advance...
You're asking a lot of questions at once, and it would be impossible for anyone to answer them all without writing your program for you, but I'll offer the high-level approach that I would take. You'll have to spend some time to dig into the details. Plenty of info on StackOverflow and otherwise to get help on these details.
Anyways-
Database, 3 tables
SeatStatus
Table
- ID, int, Primary Key
- Name, string (e.g. Available, Sold, Broken)
PricePoint
Table
- ID, int, Primary Key
- Name, string (e.g. Default, OnSale, Premium)
- Price, float (e.g. 10.0, 8.0, 12.0)
Seat
Table
- ID, int, Primary Key
- SeatStatusID, int, Foreign Key -> SeatStatus Table
- PricePointID, int, Foreign Key -> PricePoint Table
- Row, int
- Column, int
Application Model
I would use Entity Framework 4 and generate your model from your TheaterSeating database. This will create Seat
objects for you that automatically contain references to SeatStatus
and PricePoint
. Very slick.
Edit 1
Once you've created your model you will have an .edmx file that represents your data, and can read/write to your database easily. Assuming this file is called TheaterSeatingModel.edmx
, the auto-generated classes will provide you with the means to call something along the lines of:
TheaterSeatingEntities entities = new TheaterSeatingEntities();
You now have access to your entire database, including:
entities.Seats
entities.PricePoints
etc.
You can use these classes as you would naturally expect, an example:
Seat seat = new Seat();
seat.Row = 10;
seat.Column = 5;
entities.Seats.Add(seat);
entities.SaveChanges();
Populate an ObservableCollection
with these seats inside a class, and set your View's (the XAML control's) DataContext to this class.
public ObservableCollection<Seat> SeatCollection {get;set;}
public ViewModelConstructor()
{
TheaterSeatingEntities entities = new TheaterSeatingEntities();
SeatCollection = new ObservableCollection<Seat>(entities.Seats);
}
This will populate your collection with all the Seat objects.
A simple WPF binding to this collection would be something along the lines of:
<ListBox ItemsSource={Binding SeatCollection} />
UI
If it were me I would use a WPF ItemsControl
whose ItemsSource
is set to your ObservableCollection
of Seats. Place this ItemsControl
inside of a Canvas
. The ItemsControl's
ItemsControl.ItemTemplate
control what each Seat
will look like in your view. You can bind the ItemTemplates
Canvas.Left
and Canvas.Top
to the Seat.Row
and Seat.Column
to place the seats in their appropriate positions on screen. You can make the template be a button, rectangle, image of a seat, anything. You can set up Styles
and Triggers
to make the Seat display differently depending on PricePoint
, SeatStatus
, etc.
I think WPF will be great for the project you're starting.
Good luck!
精彩评论