WPF DevExpress ComboBoxEdit 实现键值绑定,输入字符自动匹配

39 篇文章 1 订阅


<Window
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:ComboxTest"
        xmlns:dxe="http://schemas.devexpress.com/winfx/2008/xaml/editors" x:Class="ComboxTest.MainWindow"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded" WindowStartupLocation="CenterScreen">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="35"></RowDefinition>
            <RowDefinition Height="Auto"></RowDefinition>
            <RowDefinition Height="35"></RowDefinition>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="100"></ColumnDefinition>
            <ColumnDefinition Width="*"></ColumnDefinition>
        </Grid.ColumnDefinitions>
        <Label Content="类型" Grid.Row="0" Grid.Column="0" HorizontalAlignment="Right" VerticalAlignment="Center"></Label>
        <dxe:ComboBoxEdit x:Name="comArea" HorizontalAlignment="Left" VerticalAlignment="Stretch"  Width="250" Height="24" Grid.Row="0" Grid.Column="1"  EditValueChanged="comArea_EditValueChanged" />
        <Label x:Name="lblArea2" Content="区域范围二:" Grid.Row="1" Grid.Column="0" Visibility="Collapsed"  HorizontalAlignment="Right" VerticalAlignment="Center"/>
        <dxe:ComboBoxEdit x:Name="comArea2" HorizontalAlignment="Left" VerticalAlignment="Stretch" Width="250" Height="24" Grid.Row="1" Grid.Column="1" EditValueChanged="comArea2_EditValueChanged"  Visibility="Collapsed"   />
      

    </Grid>
</Window>



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.Navigation;
using System.Windows.Shapes;
using System.Data;
using System.Data.SqlClient;
using Library.BLL;
using Library.Model;
using ComboxTest.Models;
namespace ComboxTest
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        R_ITArea_BLL areaBLL = new R_ITArea_BLL();
        decimal ProjectPreID = 100000003;
        int level = 0;

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                BindData();
                //  GetAreaInfoFirst();
            }
            catch (Exception)
            {

            }
        }
        DataTable dt = new DataTable();
        private void BindData()
        {
            List<ITAreaInfo> AreaList = new List<ITAreaInfo>();
            //int ParentID = 0;
            DataTable dt = areaBLL.GetITAreaFirst(ProjectPreID.ToString());
            if (dt != null && dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    ITAreaInfo type = new ITAreaInfo();

                    type.TypeName = dt.Rows[i]["TypeName"].ToString();
                    type.TypeID = dt.Rows[i]["TypeID"].ToString();

                    AreaList.Add(type);
                }
            }
            var list = AreaList;
            foreach (ITAreaInfo item in list)
            {
                this.comArea.Items.Add(item);
            }
            comArea.AutoComplete = true;//自动搜索筛选     
            comArea.ImmediatePopup = true;
            comArea.AllowDrop = true;
        }

        private void comArea_EditValueChanged(object sender, DevExpress.Xpf.Editors.EditValueChangedEventArgs e)
        {

            try
            {
                if (!string.IsNullOrEmpty(this.comArea.Text.ToString()))
                {
                    string TypeID = ((ITAreaInfo)comArea.SelectedItem).TypeID;
                    string TypeName = ((ITAreaInfo)comArea.SelectedItem).TypeName;
                    GetAreaInfoSec(TypeID);

                    level = 1;
                }
            }
            catch (Exception ex)
            {

            }
        }

        private void GetAreaInfoSec(string ParentID)
        {
            List<ITAreaInfo> AreaSecList = new List<ITAreaInfo>();
            DataTable dt = areaBLL.GetITAreaSec(ProjectPreID.ToString(), ParentID);
            if (dt != null && dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    ITAreaInfo type = new ITAreaInfo();

                    type.TypeName = dt.Rows[i]["TypeName"].ToString();
                    type.TypeID = dt.Rows[i]["TypeID"].ToString();

                    AreaSecList.Add(type);
                }
                lblArea2.Visibility = Visibility.Visible;
                comArea2.Visibility = Visibility.Visible;
                level = 2;
            }
            else
            {
                lblArea2.Visibility = Visibility.Collapsed;
                comArea2.Visibility = Visibility.Collapsed;
            }

            foreach (ITAreaInfo item in AreaSecList)
            {
                this.comArea2.Items.Add(item);
            }
            comArea2.AutoComplete = true;//自动搜索筛选     
            comArea2.ImmediatePopup = true;
            comArea2.AllowDrop = true;
        }

        private void comArea2_EditValueChanged(object sender, DevExpress.Xpf.Editors.EditValueChangedEventArgs e)
        {

        }
    }


}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ComboxTest.Models
{


    public class ITAreaInfo
    {
        public ITAreaInfo()
        {
            ITAreaInfoData = new List<ITAreaInfo>();
        }
        public string TypeName { set; get; }

        public string TypeID { set; get; }

        public override string ToString()
        {
            return TypeName;
        }
        public List<ITAreaInfo> ITAreaInfoData { get; set; }
    }



}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值