/// <summary>
/// AutoCompleteComboBox
/// </summary>
public class AutoCompleteComboBox : ComboBox
{
#region DependencyProperty
public string WaterMark
{
get { return (string)GetValue(WaterMarkProperty); }
set { SetValue(WaterMarkProperty, value); }
} // Using a DependencyProperty as the backing store for WaterMark. This enables animation, styling, binding, etc...
public static readonly DependencyProperty WaterMarkProperty =
DependencyProperty.Register("WaterMark", typeof(string), typeof(AutoCompleteComboBox), new PropertyMetadata(null, new PropertyChangedCallback(OnWaterMarkChanged))); public bool? IsNull
{
get { return (bool?)GetValue(IsNullProperty); }
set { SetValue(IsNullProperty, value); }
} // Using a DependencyProperty as the backing store for IsNull. This enables animation, styling, binding, etc...
public static readonly DependencyProperty IsNullProperty =
DependencyProperty.Register("IsNull", typeof(bool?), typeof(AutoCompleteComboBox), new PropertyMetadata(null)); public bool? SetFocuse
{
get { return (bool?)GetValue(SetFocuseProperty); }
set { SetValue(SetFocuseProperty, value); }
} // Using a DependencyProperty as the backing store for SetFocuse. This enables animation, styling, binding, etc...
public static readonly DependencyProperty SetFocuseProperty =
DependencyProperty.Register("SetFocuse", typeof(bool?), typeof(AutoCompleteComboBox), new PropertyMetadata(null, new PropertyChangedCallback(OnSetFocuseChanged))); public bool IsAllowNull
{
get { return (bool)GetValue(IsAllowNullProperty); }
set { SetValue(IsAllowNullProperty, value); }
} // Using a DependencyProperty as the backing store for IsAllowNull. This enables animation, styling, binding, etc...
public static readonly DependencyProperty IsAllowNullProperty =
DependencyProperty.Register("IsAllowNull", typeof(bool), typeof(AutoCompleteComboBox), new PropertyMetadata(true, new PropertyChangedCallback(OnIsAllowNullChanged))); private static void OnIsAllowNullChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{ } #endregion #region Event
private static void OnWaterMarkChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
{ } private static void OnSetFocuseChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
{
AutoCompleteComboBox wm = obj as AutoCompleteComboBox; if (e.NewValue != null && wm.EditableTextBox != null)
{ if ((bool)e.NewValue == true && wm.EditableTextBox.IsFocused != true)
{
wm.EditableTextBox.Focus();
} }
} public IList DataSource
{
get { return (IList)GetValue(DataSourceProperty); }
set { SetValue(DataSourceProperty, value); }
} // Using a DependencyProperty as the backing store for DataSource. This enables animation, styling, binding, etc...
public static readonly DependencyProperty DataSourceProperty =
DependencyProperty.Register("DataSource", typeof(IList), typeof(AutoCompleteComboBox), new UIPropertyMetadata(null, new PropertyChangedCallback(OnDataSourceChanged))); private static void OnDataSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
AutoCompleteComboBox a = d as AutoCompleteComboBox;
//if (a.IsAllowNull)
//{
a.Items.Clear();
if (a.DataSource != null)
{ Type b = null; foreach (var item in a.DataSource)
{
if (b == null)
{
b = item.GetType();
object o = Activator.CreateInstance(b);
//o.GetType().GetProperty(a.DisplayMemberPath).SetValue(o, "----Select----", null);
a.Items.Add(o);
}
a.Items.Add(item);
} }
else
{
a.ItemsSource = null;
} //} } protected override void OnDropDownOpened(EventArgs e)
{
base.OnDropDownOpened(e);
if (this.HasItems)
{
this.Items[].GetType().GetProperty(this.DisplayMemberPath).SetValue(this.Items[], "----Select----", null);
} } protected override void OnDropDownClosed(EventArgs e)
{
base.OnDropDownClosed(e);
if (this.SelectedIndex == )
{
//this.SelectedItem = null;
//this.SelectedItem = null;
//this.Text = null;
//this.SelectedValue = null;
this.SelectedIndex = -;
}
} public override void OnApplyTemplate()
{
base.OnApplyTemplate(); //load the text box control
if (this.EditableTextBox != null && this.MyWaterMarkTextBlock != null)
{
if (SetFocuse == true)
{
if (!this.EditableTextBox.IsFocused)
{
this.EditableTextBox.Focus();
} }
this.EditableTextBox.TextChanged += new TextChangedEventHandler(EditableTextBox_TextChanged);
this.EditableTextBox.GotFocus += EditableTextBox_GotFocus;
this.Loaded += AutoCompleteComboBox_Loaded;
} } void AutoCompleteComboBox_Loaded(object sender, RoutedEventArgs e)
{ if (string.IsNullOrWhiteSpace(this.Text))
{ this.MyWaterMarkTextBlock.Visibility = Visibility.Visible;
//this.MyWaterMarkTextBlock.Text = WaterMark;
this.IsNull = false;
}
else
{ this.MyWaterMarkTextBlock.Visibility = Visibility.Collapsed;
//this.MyWaterMarkTextBlock.Text = WaterMark;
this.IsNull = true;
}
try
{
//if (this.HasItems)
//{
// this.Items[0].GetType().GetProperty(this.DisplayMemberPath).SetValue(this.Items[0], "----Select----", null);
//}
//if (!this.IsAllowNull)
//{
// this.EditableTextBox.IsEnabled = false;
//}
}
catch (Exception)
{ } } void EditableTextBox_GotFocus(object sender, RoutedEventArgs e)
{
if (this.MyWaterMarkTextBlock.Visibility == Visibility.Visible)
{
// this.Text = null;
this.MyWaterMarkTextBlock.Visibility = Visibility.Collapsed;
//this.MyWaterMarkTextBlock.Text = WaterMark;
}
this.IsDropDownOpen = true;
} protected override void OnSelectionChanged(SelectionChangedEventArgs e)
{
base.OnSelectionChanged(e); if (this.SelectedIndex == )
{
this.SelectedIndex = -;
} } void EditableTextBox_TextChanged(object sender, TextChangedEventArgs e)
{
if (this.EditableTextBox.IsFocused && this.IsDropDownOpen == false)
{ this.IsDropDownOpen = true;
}
if (string.IsNullOrWhiteSpace(this.Text))
{
if (!this.EditableTextBox.IsFocused)
{
this.MyWaterMarkTextBlock.Visibility = Visibility.Visible;
// this.MyWaterMarkTextBlock.Text = WaterMark; }
this.IsNull = false; }
else
{
this.IsNull = true;
this.MyWaterMarkTextBlock.Visibility = Visibility.Collapsed;
// this.MyWaterMarkTextBlock.Text = WaterMark; } } void SimpleAutoCompleteComboBox_LostFocus(object sender, RoutedEventArgs e)
{
this.IsDropDownOpen = false;
// to prevent misunderstanding that user has entered some information
if (this.SelectedIndex == - || this.SelectedIndex == )
{
this.Text = null;
this.SelectedItem = null;
this.SelectedValue = null;
this.MyWaterMarkTextBlock.Visibility = Visibility.Visible;
this.IsNull = false;
// this.MyWaterMarkTextBlock.Text = WaterMark; }
// syncronize text
else
{
this.Text = this.SelectedText;
this.IsNull = true;
}
// release timer resources try
{
this.EditableTextBox.CaretIndex = ;
}
catch { }
} #endregion #region Construtor
static AutoCompleteComboBox()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(AutoCompleteComboBox), new FrameworkPropertyMetadata(typeof(AutoCompleteComboBox))); } public AutoCompleteComboBox()
{
this.StaysOpenOnEdit = true;
this.IsEditable = true;
this.IsTextSearchEnabled = true;
this.LostFocus += SimpleAutoCompleteComboBox_LostFocus;
this.IsAllowNull = true; }
#endregion protected override void OnItemsSourceChanged(System.Collections.IEnumerable oldValue, System.Collections.IEnumerable newValue)
{
base.OnItemsSourceChanged(oldValue, newValue);
} #region property
/// <summary>
/// Gets the waterMark .
/// </summary>
protected TextBlock MyWaterMarkTextBlock
{
get
{
return base.GetTemplateChild("PART_MyWaterMarkTextBlock") as TextBlock;
}
} /// <summary>
/// Gets the text box in charge of the editable portion of the combo box.
/// </summary>
protected TextBox EditableTextBox
{
get
{
return base.GetTemplateChild("PART_EditableTextBox") as TextBox;
}
} private string SelectedText
{
get
{
try
{
if (this.SelectedIndex == -) return null; if (this.SelectedItem != null)
{
return this.SelectedItem.GetType().GetProperty(this.DisplayMemberPath).GetValue(this.SelectedItem, null).ToString();
}
else
{
return null;
}
}
catch (System.Exception)
{ return null;
} }
}
#endregion }

最新文章

  1. Linux测试环境部署jdk(一)
  2. Asp.Net 高性能ORM框架 SqlSugar.ORM 2.8
  3. 《大道至简-Team》
  4. 使用iOS手势UIGestureRecognizer
  5. C# 网页图片爬虫的几种技术基础
  6. YKCW6-BPFPF-BT8C9-7DCTH-QXGWCYQ7PR-QTHDM-HCBCV-9GKGG-TB2TM
  7. linux c setitimer使用方法说明
  8. vim Diff,Easy,Read-Only 的区别
  9. Emacs助力PowerShell
  10. HDU1201 水题
  11. 关于maven的配置使用 这一篇还比较全 2017.12.13
  12. vue-cli3.0 gui(一)
  13. django使用ckeditor上传图片
  14. js实现图片查看器(图片的缩放、旋转、拖拽)
  15. Spring事务管理2--声明式
  16. 3386 二分图 洛谷luogu [模版]
  17. 【bzoj3876】 Ahoi2014—支线剧情
  18. 配置GitLab Push 自动触发Jenkins构建
  19. Oracle清理回收站的方法
  20. (转)Spring实现IoC的多种方式

热门文章

  1. [转载]Python-第三方库requests详解
  2. 【leetcode】Convert Sorted Array to Binary Search Tree
  3. ios 中使用https的知识
  4. android 和iOS的view上的区别
  5. Qt字符转换
  6. Linux基础命令总结
  7. 和我一起学python,初识python (life is short ,we need python)
  8. ACM/ICPC 之 两道dijkstra练习题(ZOJ1053(POJ1122)-ZOJ1053)
  9. pt-query-digest使用介绍【转】
  10. 用 MyEclipse 开发 Spring 入门操作