Files
BeWoPlaner/Controls/Controls/NullItemComboBox.cs

72 lines
2.4 KiB
C#
Raw Normal View History

2016-06-27 01:45:38 +02:00
using System.Collections;
using System.IO;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Markup;
namespace BeWo.Controls
{
public class NullItemComboBox : ComboBox
{
public static readonly DependencyProperty NullTextProperty = DependencyProperty.Register("NullText", typeof(string), typeof(NullItemComboBox), new UIPropertyMetadata(string.Empty));
private string l;
public static DataTemplate NullTextItemTemplate
{
get
{
const string lXaml = "<DataTemplate><Label Content=\"{Binding}\" /></DataTemplate>";
2016-06-27 01:45:38 +02:00
var lParserContext = new ParserContext();
lParserContext.XmlnsDictionary.Add(string.Empty, "http://schemas.microsoft.com/winfx/2006/xaml/presentation");
lParserContext.XmlnsDictionary.Add("x", "http://schemas.microsoft.com/winfx/2006/xaml");
return (DataTemplate)XamlReader.Load(new MemoryStream(Encoding.ASCII.GetBytes(lXaml)), lParserContext);
}
}
public string NullText
{
get => (string)GetValue(NullTextProperty);
2016-06-27 01:45:38 +02:00
set => SetValue(NullTextProperty, value);
2016-06-27 01:45:38 +02:00
}
protected override void OnItemsSourceChanged(IEnumerable oldValue, IEnumerable newValue)
{
this.l = this.NullText; // " ... " + NullText + " ...";
2017-07-25 10:52:20 +02:00
IEnumerator lEnum = null;
if (newValue != null)
{
lEnum = newValue.GetEnumerator();
}
if (lEnum != null && lEnum.MoveNext() && !lEnum.Current.Equals(this.l))
2016-06-27 01:45:38 +02:00
{
ArrayList lTemp = new ArrayList();
lTemp.Add(this.l);
do
{
lTemp.Add(lEnum.Current);
}
while (lEnum.MoveNext());
this.ItemsSource = lTemp;
}
else
{
base.OnItemsSourceChanged(oldValue, newValue);
}
}
protected override void OnSelectionChanged(SelectionChangedEventArgs e)
{
if(SelectedItem != null && SelectedItem.Equals(l))
2016-06-27 01:45:38 +02:00
{
SelectedIndex = -1;
2016-06-27 01:45:38 +02:00
}
base.OnSelectionChanged(e);
}
}
}