WPF绑定到linq表达式

using ClassLibrary;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;


namespace CollectionBinding
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }


        public ObservableCollection<Product> products;
        public IEnumerable<Product> matches;


        private void btnGetProducts_Click_1(object sender, RoutedEventArgs e)
        {
            decimal min = Convert.ToDecimal(txtMinUniCost.Text);
            products = StoreDB.GetProducts();
            matches = from p in products where p.UnitCost > min select p;
            lstProducts.ItemsSource = matches;
            lstProducts.DisplayMemberPath = "ModelName";
        }


        private void btnDelete_Click_1(object sender, RoutedEventArgs e)
        {
            Product p = (Product)lstProducts.SelectedItem;
            products.Remove(p);
            StoreDB.DeleteProductByID(p.ProductID);
        }


        private void btnInsert_Click_1(object sender, RoutedEventArgs e)
        {
            int categoryID = Convert.ToInt32(txtCategoryID.Text);
            decimal unitCost = Convert.ToDecimal(txtUnitCost.Text);


            Product p = new Product() { CategoryID = categoryID, ModelNumber = txtModelNumber.Text, ModelName = txtModelName.Text, ProductImage = txtProductImage.Text, UnitCost = unitCost, Description = txtDescription.Text };
            StoreDB.InsertProduct(p);
            products.Add(p);
        }




    }
}
posted @ 2018-03-30 08:32  dxm809  阅读(155)  评论(0编辑  收藏  举报