wpf商品销售ui界面和控制器代码
时间: 2024-05-02 07:03:28 浏览: 111
以下是一个简单的 WPF 商品销售 UI 界面和控制器代码:
MainWindow.xaml:
```xml
<Window x:Class="WpfApp1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="商品销售" Height="450" Width="800">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="2*" />
</Grid.ColumnDefinitions>
<StackPanel Grid.Column="0" Margin="10">
<TextBlock Text="商品列表" FontWeight="Bold" FontSize="18" Margin="0 0 0 10" />
<ListBox Name="lbProducts" DisplayMemberPath="Name" SelectionChanged="lbProducts_SelectionChanged" />
</StackPanel>
<StackPanel Grid.Column="1" Margin="10">
<TextBlock Text="购物车" FontWeight="Bold" FontSize="18" Margin="0 0 0 10" />
<ListBox Name="lbCart" DisplayMemberPath="Name" />
<StackPanel Margin="0 10 0 0">
<TextBlock Text="总价:" FontWeight="Bold" FontSize="16" />
<TextBlock Name="tbTotalPrice" FontWeight="Bold" FontSize="16" />
</StackPanel>
<Button Content="结算" Margin="0 10 0 0" Click="btnCheckout_Click" />
</StackPanel>
</Grid>
</Window>
```
MainWindow.xaml.cs:
```csharp
using System.Collections.Generic;
using System.Linq;
using System.Windows;
namespace WpfApp1
{
public partial class MainWindow : Window
{
private List<Product> products = new List<Product>
{
new Product { Id = 1, Name = "商品1", Price = 10 },
new Product { Id = 2, Name = "商品2", Price = 20 },
new Product { Id = 3, Name = "商品3", Price = 30 },
new Product { Id = 4, Name = "商品4", Price = 40 },
new Product { Id = 5, Name = "商品5", Price = 50 },
};
private List<Product> cart = new List<Product>();
public MainWindow()
{
InitializeComponent();
lbProducts.ItemsSource = products;
}
private void lbProducts_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{
var product = lbProducts.SelectedItem as Product;
if (product != null)
{
cart.Add(product);
lbCart.ItemsSource = null;
lbCart.ItemsSource = cart;
tbTotalPrice.Text = cart.Sum(p => p.Price).ToString();
}
}
private void btnCheckout_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("结算成功!");
cart.Clear();
lbCart.ItemsSource = null;
tbTotalPrice.Text = "";
}
}
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
}
}
```
在这个示例中,我们创建了一个窗口,分为两个列。左边的列显示商品列表,右边的列显示购物车和总价。
当用户选择一个商品时,我们将其添加到购物车中,并更新购物车和总价的显示。当用户点击“结算”按钮时,我们弹出一个消息框,清空购物车并重置显示。
阅读全文
相关推荐
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![application/x-zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)