【问题标题】:wpf combobox - performance after changing bindingwpf 组合框 - 更改绑定后的性能
【发布时间】:2013-08-16 13:45:04
【问题描述】:

我有两个组合框。两者的项目都是枚举值。根据 comboFoo 中的选择,comboBar 中有不同的可用项目(绑定更改)。第一次 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、Debug-Build 和 Release-Build 测试

【问题讨论】:

  • 我已经复制了您的代码,现在使用 ComboBox 或 ListBox 可以看到明显的减速。
  • 好吧,这很奇怪。你使用 .net4 还是 4.5?
  • 哦,我读过 no 明显的减速,但你写的 now 明显减速...但这也很奇怪... . 在我的项目中使用列表框没问题?!
  • @RuDAir 我的意思是 no 减速(我的错字)。我使用.Net 4.5
  • 可能是 4.0 的错误...我稍后会在家里检查...@work 我不允许在我的计算机上安装任何东西...谢谢

标签: c# wpf performance binding combobox


【解决方案1】:

好的,绑定问题似乎只出现在带有 .NET4 的 Windows Vista 上——可能是组合框本身的一个错误。上面的代码很好。

我测试过:

  • win8、.net 4.5、vs2012:没问题
  • win8、.net 4.0、vs2012:没问题
  • win7、.net 4.0、vs2010:没问题(在我同事的电脑上)
  • vista、.net 4.0、vs2010:问题(在另一位同事的计算机和我的计算机上)

【讨论】:

  • 您是否有可能在那里使用非最终版本的 .Net 4? RC 版本存在一些性能问题。
  • 最终版本 (4.0.30319.1)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-04-04
  • 1970-01-01
  • 2012-06-18
  • 2011-03-09
相关资源
最近更新 更多