csharp WPF SearchDialog

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了csharp WPF SearchDialog相关的知识,希望对你有一定的参考价值。

using System;
using System.Collections.Generic;
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.Shapes;
using dbConnection;

namespace WpfApp1
{
    /// <summary>
    /// Interaction logic for SupplierDialog.xaml
    /// </summary>
    /*
        var sup = new SupplierDialog();
        var result = sup.ShowDialog();
        if (result == true)
        {
          MessageBox.Show(sup.Code);
        }
    */
    public partial class SupplierDialog : Window
    {
        dbConnectionDB _db = new dbConnectionDB();
        public string Code;
        public SupplierDialog()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            System.Windows.Data.CollectionViewSource suppliers_mstViewSource = ((System.Windows.Data.CollectionViewSource)(this.FindResource("suppliers_mstViewSource")));
            // Load data by setting the CollectionViewSource.Source property:
            // suppliers_mstViewSource.Source = [generic data source]
            suppliers_mstViewSource.Source = _db.Query<suppliers_mst>("SELECT * FROM suppliers_mst").ToList();

            suppliers_mstDataGrid.CanUserAddRows = false;
            suppliers_mstDataGrid.CanUserDeleteRows = false;
            suppliers_mstDataGrid.IsReadOnly = true;
            if (suppliers_mstDataGrid.Items.Count > 0)
            {
                var view = (CollectionView)CollectionViewSource.GetDefaultView(suppliers_mstDataGrid.ItemsSource);
                view.Filter = SearchFilter;
            }
            EventManager.RegisterClassHandler(typeof(Window),
                Keyboard.PreviewKeyDownEvent, new KeyEventHandler(keyDown), true);
            tbSearch.Focus();
        }

        private bool StudentFilter(object item)
        {
            if (string.IsNullOrEmpty(tbSearch.Text))
                return true;
            else
            {
                var code =
                    (((suppliers_mst)item).Supplier_Code.IndexOf(tbSearch.Text, StringComparison.OrdinalIgnoreCase) >= 0);
                var name =
                    (((suppliers_mst)item).Supplier_Name.IndexOf(tbSearch.Text, StringComparison.OrdinalIgnoreCase) >= 0);
                return code || name;
            }
        }

        private void tbSearch_TextChanged(object sender, TextChangedEventArgs e)
        {
            if(suppliers_mstDataGrid != null)
                CollectionViewSource.GetDefaultView(suppliers_mstDataGrid.ItemsSource).Refresh(); // Filter
        }

        private void suppliers_mstDataGrid_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            OK();
        }

        private void OK()
        {
            if (suppliers_mstDataGrid.SelectedItem == null)
            {
                this.Close();
                return;
            }
            Code = ((suppliers_mst) suppliers_mstDataGrid.SelectedItem).Supplier_Code;
            if (this.DialogResult == null)
            {
                this.DialogResult = true;
            }
            this.Close();
        }

        private void keyDown(object sender, KeyEventArgs e)
        {
            switch (e.Key)
            {
                case Key.Enter:
                    e.Handled = true; // TO CANCEL KEY KEEPING DOWN
                    OK();
                    return;
                case Key.Escape:
                    this.Close();
                    return;
            }
            var ctrlF = (Keyboard.Modifiers == ModifierKeys.Control) && e.Key == Key.F;
            if (ctrlF) tbSearch.Focus();
        }

    }
}

以上是关于csharp WPF SearchDialog的主要内容,如果未能解决你的问题,请参考以下文章

csharp WPF

csharp WPF数据网格CRUD

csharp WPF为元素生成图片

csharp WPF鼠标穿透

csharp WPF对指定控件发送按键

csharp WPF注册系统热键