您的位置:首页 > 其它

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

2016-09-27 17:11 786 查看

<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; }
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: