WPF 中的剧院座位应用程序 - 数据绑定问题

发布于 2024-09-26 19:28:55 字数 290 浏览 4 评论 0原文

我是 WPF 新手,我想在 WPF-c#.net 中开发一个用于剧院座位的应用程序。 我做了一些研究,但我迷失了,因为我不知道每个座位的数据(是否出售、价格、在哪一行)最终应如何绑定到 SQL Express 中表中的字段。 我应该使用 ObservableCollection 来填充其中数据表的内容吗? 另外,我应该使用按钮网格吗?还是矩形?如果我希望左前和右前按钮稍微倾斜而不是水平,我应该如何操作? 如果我想要三个区域的座椅,我应该使用包裹板吗?左边区域,修改空间,中间区域,修改空间,右边区域? 如果有人能给我一些开始的提示,我会非常高兴。 提前致谢...

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 if 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...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清晨说晚安 2024-10-03 19:28:55

您同时提出了很多问题,如果不为您编写程序,任何人都不可能回答所有问题,但我将提供我将采取的高级方法。您必须花一些时间来深入研究细节。有关 StackOverflow 的大量信息以及其他有关这些详细信息的帮助信息。

无论如何 -

数据库,3 个表

SeatStatus

  • ID,int,主键
  • 名称,字符串(例如可用,
    已售出、已损坏)

PricePoint

  • ID、int、主键
  • 名称、字符串(例如默认值、
    OnSale、Premium)
  • 价格、浮动(例如 10.0、8.0、12.0)

Seat

  • ID,int,主键
  • SeatStatusID,int,外键 ->
    SeatStatus 表
  • PricePointID, int, 外键 ->
    PricePoint Table
  • Row, int
  • Column, int

应用程序模型

我将使用Entity Framework 4 并从 TheaterSeating 数据库生成模型。这将为您创建 Seat 对象,其中自动包含对 SeatStatusPricePoint 的引用。非常光滑。

编辑 1

创建模型后,您将拥有一个代表数据的 .edmx 文件,并且可以轻松读取/写入数据库。假设此文件名为 TheaterSeatingModel.edmx,自动生成的类将为您提供调用以下内容的方法:

TheaterSeatingEntities entities = new TheaterSeatingEntities();

您现在可以访问整个数据库,包括:

entities.Seats
entities.PricePoints

等等。

您可以按照您自然期望的方式使用这些类,例如:

Seat seat = new Seat();
seat.Row = 10;
seat.Column = 5;
entities.Seats.Add(seat);
entities.SaveChanges();

在类中使用这些席位填充 ObservableCollection,并将您的视图(XAML 控件)的 DataContext 设置为此类。

public ObservableCollection<Seat> SeatCollection {get;set;}

public ViewModelConstructor()
{
   TheaterSeatingEntities entities = new TheaterSeatingEntities();
   SeatCollection = new ObservableCollection<Seat>(entities.Seats);
}

这将使用所有 Seat 对象填充您的集合。

与此集合的简单 WPF 绑定将类似于:

<ListBox ItemsSource={Binding SeatCollection} />

UI

如果是我,我会使用 WPF ItemsControl ,其 ItemsSource 是设置为您的 ObservableCollection 席位。将此 ItemsControl 放置在 Canvas 内。 ItemsControl 的 ItemsControl.ItemTemplate 控制每个Seat 在您的视图中的外观。您可以将 ItemTemplates Canvas.LeftCanvas.Top 绑定到 Seat.RowSeat .Column 将座位放置在屏幕上适当的位置。您可以将模板设为按钮、矩形、座位图像等任何内容。您可以设置 StylesTriggers 以使座位根据 PricePointSeatStatus 等以不同方式显示

。认为 WPF 对于您正在启动的项目非常有用。

祝你好运!

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文