我有两个组合框。这两个项都是枚举值。根据选择的组合富有在组合酒吧(结合其他城市)可用不同的项目。第一次comboBar总是很快弹出 - 无论是否comboFoo已经弹出或没有弹出。但在comboFoo(在10 comboBar弹出后至少有一次)的每个选择更改后,弹出非常缓慢的组合条形图comboBar。我不知道如何解决它。wpf combobox - 更改绑定后的性能
枚举
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,Debug-Build和Release-Build testing
我已经转载您的代码,并通过使用ComboBox或ListBox来查看现在明显的减速。 –
好的,这很奇怪。你使用.net4还是4.5? –
哦,我读过** no **不明显的减速,但是你现在已经写**了**明显的减速......但这也很奇怪......使用列表框在我的项目中没有问题? –