为什么当项目源更改时我的组合框会冻结?

发布于 2024-10-04 02:24:16 字数 1567 浏览 0 评论 0原文

重新生成的步骤:

当应用程序启动时,打开组合框以便生成项目。现在点击“点击我!”按钮。在后面的代码中,组合框的项目源已更改。现在尝试再次打开组合框。即使绑定集合中只有 2 个项目,组合框也会冻结至少 5 秒。这只是一个测试应用程序。在我的实际应用中,有超过2个项目,并且滞后是难以忍受的。我已经尝试过打开和关闭虚拟化。这没有什么区别。

什么事要花这么长时间?我该如何解决这个问题?如果没有直接解决办法,有解决办法吗?

XAML:

    <StackPanel>
        <ComboBox x:Name="cbo" DisplayMemberPath="Junk1"></ComboBox>
        <Button Content="Click Me!" Click="btn_Click"></Button>
    </StackPanel>

代码:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        this.Loaded += new RoutedEventHandler(MainWindow_Loaded);
    }

    ObservableCollection<Junk> junk1 = new ObservableCollection<Junk>() {
        new Junk() { Junk1 = "jdkf", Junk2 = "fjdfkasjd;klfj" },
        new Junk() { Junk1 = "jfdk;a", Junk2 = "fjkdljf" } };

    ObservableCollection<Junk> junk2 = new ObservableCollection<Junk>() {
        new Junk() { Junk1 = "fjkdfhsdjk", Junk2 = "fdjkah;" },
        new Junk() { Junk1="", Junk2 = "asdfj" } };

    void MainWindow_Loaded(object sender, RoutedEventArgs e)
    {
        this.cbo.ItemsSource = junk1;
    }

    private void btn_Click(object sender, RoutedEventArgs e)
    {
        if (this.cbo.ItemsSource == junk1)
            this.cbo.ItemsSource = junk2;
        else
            this.cbo.ItemsSource = junk1;
        this.cbo.UpdateLayout();
    }
}

public class Junk
{
    public string Junk1 { get; set; }
    public string Junk2 { get; set; }
}

Steps to Reporoduce:

When the app starts, open the combobox so the items get generated. Now click the "Click Me!" button. In the code behind, the itemssource of the combobox is changed. Now try to open the combobox again. The combobox freezes for at least 5 seconds even though only 2 items are in the bound collection. This is just a test app. In my real application, there are more than 2 items and the lag is unbearable. I've tried this with virtualizing on and off. It makes no difference.

What is taking so long? How do I fix this? If there's no direct fix, is there a work around?

XAML:

    <StackPanel>
        <ComboBox x:Name="cbo" DisplayMemberPath="Junk1"></ComboBox>
        <Button Content="Click Me!" Click="btn_Click"></Button>
    </StackPanel>

CODE:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        this.Loaded += new RoutedEventHandler(MainWindow_Loaded);
    }

    ObservableCollection<Junk> junk1 = new ObservableCollection<Junk>() {
        new Junk() { Junk1 = "jdkf", Junk2 = "fjdfkasjd;klfj" },
        new Junk() { Junk1 = "jfdk;a", Junk2 = "fjkdljf" } };

    ObservableCollection<Junk> junk2 = new ObservableCollection<Junk>() {
        new Junk() { Junk1 = "fjkdfhsdjk", Junk2 = "fdjkah;" },
        new Junk() { Junk1="", Junk2 = "asdfj" } };

    void MainWindow_Loaded(object sender, RoutedEventArgs e)
    {
        this.cbo.ItemsSource = junk1;
    }

    private void btn_Click(object sender, RoutedEventArgs e)
    {
        if (this.cbo.ItemsSource == junk1)
            this.cbo.ItemsSource = junk2;
        else
            this.cbo.ItemsSource = junk1;
        this.cbo.UpdateLayout();
    }
}

public class Junk
{
    public string Junk1 { get; set; }
    public string Junk2 { get; set; }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我的影子我的梦 2024-10-11 02:24:16

myermian - 没有连接其他事件。这是整个应用程序。不需要其他代码即可获得冻结行为。

Aaron - 垃圾只是一个标准类别。它不源于任何东西。我尝试保留集合,调用 .Clear() 方法,然后添加新项目。我得到完全相同的行为。

AnthonyWJones - 该错误存在于 WPF 中。我没有在 Silverlight 中尝试过。抱歉造成混乱。

myermian - No other events are wired. This is the entire application. There is no other code required to get the freezing behavior.

Aaron - Junk is just a standard class. It does not derive from anything. I have tried keeping the collection, calling the .Clear() method and then adding the new items. I get the exact same behavior.

AnthonyWJones - The bug is in WPF. I hadn't tried it in Silverlight. Sorry for the confusion.

彡翼 2024-10-11 02:24:16

我已经准确地复制了您的代码,并且它按预期完美运行 - 所以我建议您的环境可能有问题。

注意:您不需要调用 this.cbo.UpdateLayout();,因为 ItemsSource 是一个依赖属性,并且会在更改时自动更新控件。

I have copied your code exactly, and it runs perfectly as expected - so I suggest there may be something wrong with your environment.

Note : you do not need to call this.cbo.UpdateLayout(); as ItemsSource is a dependency property and will update the control automatically when changed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文