当前位置:网站首页>WPF如何自定义隐藏下拉框选项
WPF如何自定义隐藏下拉框选项
2022-08-01 14:10:00 【InfoQ】
<ComboBox IsTextSearchEnabled="False"
IsEditable="True"
ItemsSource="source" <!--source {value:string, text:string }-->
KeyUp="comboBox_KeyUp" DropDownOpened="OnDropDownOpened" DropDownClosed="OnDropDownClosed"
/>
/// <summary>
/// 搜索条件过滤
/// </summary>
private void comboBox_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
{
var combobox = (ComboBox)sender;
combobox.SelectedItem = null;
var ctb = combobox.Template.FindName("PART_EditableTextBox", combobox) as TextBox;
if (ctb == null) return;
if (Keyboard.Modifiers.HasFlag(ModifierKeys.Shift) || Keyboard.Modifiers.HasFlag(ModifierKeys.Control) || Keyboard.Modifiers.HasFlag(ModifierKeys.Alt))
return;
var caretPos = ctb.CaretIndex;
combobox.IsDropDownOpen = true;
var datasource = (combobox.ItemsSource as List<SelectOption>).DeepClone().Where(x => /*自定义条件*/)).ToList();
combobox.ItemsSource = datasource;
CollectionView itemsViewOriginal = (CollectionView)CollectionViewSource.GetDefaultView(combobox.Items);
itemsViewOriginal.Filter = ((o) =>
{
if (String.IsNullOrEmpty(combobox.Text))
{
return true;
}
else
{
if (((SelectOption)o).text.ToLower().Contains(combobox.Text.ToLower()))//(SelectOption)o).text.StartsWith(combobox.Text, true, null)
{
return true;
}
else
{
return false;
}
}
});
itemsViewOriginal.Refresh();
ctb.CaretIndex = caretPos;
}
/// <summary>
/// 下拉框初始化过滤
/// </summary>
private void OnDropDownOpened(object sender, EventArgs e)
{
var cb = (ComboBox)sender;
if (cb.IsDropDownOpen == true)
{
var combobox = (ComboBox)sender;
//combobox.SelectedItem = null;
var ctb = combobox.Template.FindName("PART_EditableTextBox", combobox) as TextBox;
if (ctb == null) return;
if (Keyboard.Modifiers.HasFlag(ModifierKeys.Shift) || Keyboard.Modifiers.HasFlag(ModifierKeys.Control) || Keyboard.Modifiers.HasFlag(ModifierKeys.Alt))
return;
var caretPos = ctb.CaretIndex;
combobox.IsDropDownOpen = true;
var tempValue = combobox.SelectedValue;
var datasource = (combobox.ItemsSource as List<SelectOption>).DeepClone()
.Where(x => /*自定义条件*/)
.ToList();
combobox.ItemsSource = datasource;
CollectionView itemsViewOriginal = (CollectionView)CollectionViewSource.GetDefaultView(combobox.Items);
itemsViewOriginal.Refresh();
combobox.SelectedValue = tempValue;
ctb.CaretIndex = caretPos;
}
}
/// <summary>
/// 下拉框数据源还原
/// </summary>
private void OnDropDownClosed(object sender, EventArgs e)
{
var cb = (ComboBox)sender;
if (cb.IsDropDownOpen == false)
{
var tempValue = cb.SelectedValue;
cb.ItemsSource = (this.DataContext as /*定义的前端ViewModel*/).source;
cb.SelectedValue = tempValue;
}
}
结语
边栏推荐
猜你喜欢
免费使用高性能的GPU和TPU—谷歌Colab使用教程
Gradle系列——Gradle测试,Gradle生命周期,settings.gradle说明,Gradle任务(基于Groovy文档4.0.4)day2-3
响应式2022英文企业官网源码,感觉挺有创意的
OpenSSL SSL_read: Connection was reset, errno 10054
【5GC】5G网络切片与5G QoS的区别?
倪光南:openEuler已达国际同类社区水准
透过现象看本质,如何针对用户做好需求分析
Wovent Bio IPO: Annual revenue of 480 million pension fund is a shareholder
【每日一题】1331. 数组序号转换
gpio analog serial communication
随机推荐
大佬们,datax同步数据,同步过程中要新增一个uuid,请问column 怎么写pgsql,uu
高仿项目协作工具【Worktile】,从零带你一步步实现组织架构、网盘、消息、项目、审批等功能
经纬信息IPO过会:年营收3.5亿 叶肖华控制46.3%股权
使用open3d可视化3d人脸
HTB-Shocker
论文笔记All about Eve: Execute-Verify Replication for Multi-Core Servers
ThreadLocal保存用户登录信息
VIP的实现原理
D - I Hate Non-integer Number(背包dp)
Gradle series - Gradle tests, Gradle life cycle, settings.gradle description, Gradle tasks (based on Groovy documentation 4.0.4) day2-3
长江欧拉生态创新中心成立,武汉数字经济再添坚实底座
D - Draw Your Cards (Simulation)
[Binary Tree] Path Sum II
Multithreading Case - Timer
性能测试入门指南
微服务原生案例搭建
我寻找的方向
阿里巴巴测试开发岗P6面试题
A Beginner's Guide to Performance Testing
opencv 保存图片imwrite