[英]wpf combobox - performance after changing binding
我有兩個組合框。 兩者的項目都是枚舉值。 根據組合 Foo中的選擇, 組合 欄中可以使用不同的項目(裝訂更改)。 第一次comboBar總是快速彈出-無論comboFoo是否之前彈出。 但是,在comboFoo中的每個選擇更改之后 (在comboBar彈出至少一次之后), comboBar彈出非常緩慢。 我不知道如何解決它。
枚舉
public enum Foo
{
Foo1, Foo2, Foo3
}
public enum Bar
{
Bar1, Bar2, Bar3, Bar4, Bar5, Bar6, Bar7, Bar8, Bar9, Bar10,
Bar11, Bar12, Bar13, Bar14, Bar15, Bar16, Bar17, Bar18,
}
標記為枚舉
public class EnumValuesExtension : MarkupExtension
{
private readonly Type enumType;
public EnumValuesExtension(Type enumType)
{
if (enumType == null)
throw new ArgumentNullException("enumType");
if (!enumType.IsEnum)
throw new ArgumentException("Argument enumType must derive from type Enum.");
this.enumType = enumType;
}
public override object ProvideValue(IServiceProvider serviceProvider)
{
return Enum.GetValues(enumType);
}
}
轉換器 (用法請參見下面的方法)
public class EnumToFilteredListConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (!(value is Type))
return null;
var enumType = (Type)value;
if (!enumType.IsSubclassOf(typeof(Enum)))
return null;
Array allValues = Enum.GetValues(enumType);
IEnumerable enumList;
var filterList = (parameter == null) ? Enum.GetValues(enumType).Cast<Enum>() : (parameter as Array).Cast<Enum>();
try
{
enumList = from Enum enumValue in allValues
where filterList.Contains(enumValue)
select enumValue;
}
catch (ArgumentNullException)
{
enumList = allValues;
}
catch (ArgumentException)
{
enumList = allValues;
}
return enumList;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
SelectionChanged方法 (后面的代碼)
private void cboFoo_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (comboBar == null)
return;
if (comboFoo.SelectedItem == null)
return;
var binding = new Binding
{
Source = typeof(Bar),
Converter = new EnumToFilteredListConverter()
};
switch ((Foo)comboFoo.SelectedItem)
{
case Foo.Foo1: // Show only Bar1-Bar3
binding.ConverterParameter = new Enum[] { Bar.Bar1, Bar.Bar2, Bar.Bar3 };
break;
case Foo.Foo2: // Show only Bar3-Bar5
binding.ConverterParameter = new Enum[] { Bar.Bar3, Bar.Bar4, Bar.Bar5 };
break;
default: // Show all of Bar
binding.ConverterParameter = null;
break;
}
comboBar.SetBinding(ItemsControl.ItemsSourceProperty, binding);
}
XAML
<StackPanel>
<ComboBox Name="comboFoo" ItemsSource="{Binding Source={markup:EnumValues {x:Type types:Foo}}}" SelectionChanged="cboFoo_SelectionChanged" />
<ComboBox Name="comboBar" ItemsSource="{Binding Source={markup:EnumValues {x:Type types:Bar}}}" />
<!--<ListBox Name="comboBar" ItemsSource="{Binding Source={markup:EnumValues {x:Type types:Bar}}}" />-->
</StackPanel>
如果我使用列表框而不是comboBar ,那么我沒有任何性能問題,因此我認為問題不屬於轉換器...
PS:VS2010,.NET4.0,調試版本以及發布版本均經過測試
好的,綁定問題似乎僅在具有.NET4的Windows Vista上-可能是combobox本身的錯誤。 上面的代碼很好。
我用以下方法進行了測試:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.