首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >wpf组合框-更改绑定后的性能

wpf组合框-更改绑定后的性能
EN

Stack Overflow用户
提问于 2013-08-16 13:45:04
回答 1查看 225关注 0票数 0

我有两个组合框。这两个项目都是枚举值。根据组合框Foo中的选择,组合框Bar(绑定更改)中有不同的项可用。第一次comboBar总是弹出的很快--不管comboFoo是否曾经出现过。但是,在comboFoo中的每一个选择更改之后( comboBar至少弹出一次之后),comboBar就会缓慢地弹出。我不知道怎么修理它。

Enum

代码语言:javascript
复制
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,
}

标记用于枚举

代码语言:javascript
复制
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);
    }
}

转换器(用法见下面的方法)

代码语言:javascript
复制
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 method (代码背后)

代码语言:javascript
复制
    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

代码语言:javascript
复制
<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和同时发布的构建测试

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-08-19 08:03:51

好的,绑定问题似乎只出现在Windows和.NET4上--可能是combobox本身的一个bug。上面的代码很好。

我用以下方法进行了测试:

  • win8,.net 4.5,vs2012:没问题
  • win8,.net 4.0,vs2012:没问题
  • win7,.net 4.0,vs2010:没问题(在我同事的电脑上)
  • vista,.net 4.0,vs2010:problem (在另一位同事的计算机上和在我的计算机上)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18274821

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档