Files
BeWoPlaner/Controls/Controls/NullItemComboBox.cs

72 lines
2.4 KiB
C#

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>";
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);
set => SetValue(NullTextProperty, value);
}
protected override void OnItemsSourceChanged(IEnumerable oldValue, IEnumerable newValue)
{
this.l = this.NullText; // " ... " + NullText + " ...";
IEnumerator lEnum = null;
if (newValue != null)
{
lEnum = newValue.GetEnumerator();
}
if (lEnum != null && lEnum.MoveNext() && !lEnum.Current.Equals(this.l))
{
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))
{
SelectedIndex = -1;
}
base.OnSelectionChanged(e);
}
}
}