将 ObservableCollection 绑定到 ItemsControl - 并不像听起来那么简单?

发布于 2024-12-16 13:04:13 字数 2070 浏览 1 评论 0原文

这非常难以追踪,并且给我带来了很大的痛苦 - 但似乎 ItemsControls 的行为并不符合我的预期。它几乎看起来像是 WPF 中的一个错误 - 但作为 WPF 的新手,我错误地认为这是我的错,而不是他们的错。

重现它非常简单 - 将 ItemsControl 绑定到 ObservableCollection,然后替换集合中的项目。这太简单了,我不敢相信谷歌没有找到成千上万有同样问题的人。

下面的代码只是将 ItemsControl 绑定到 BrushObservableCollection。更改画笔(通过单击按钮),您会收到一些数据错误,因为矩形的画笔绑定暂时属于 ItemsControlDataContext(!),而不是比新项目。每当我替换集合中的(不可变的常规 CLR 对象)项时,绑定的短暂崩溃都会导致我的应用程序在调试器中运行时需要半秒多的时间来更新 - 我做错了什么?

<Window x:Class="Dummy.Test"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Test" Height="300" Width="300">
    <Grid>
        <ItemsControl ItemsSource="{Binding Foos}">
            <ItemsControl.ItemTemplate>
                <DataTemplate DataType="{x:Type Brush}">
                    <Rectangle Width="20" Height="20" Fill="{Binding}" />
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
        <Button HorizontalAlignment="Center" VerticalAlignment="Bottom" Click="SwitchClick">Switch</Button>
    </Grid>
</Window>
using System;
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Media;

namespace Dummy
{
    public partial class Test : Window
    {
        private readonly ObservableCollection<Brush> foos = new ObservableCollection<Brush>();
        public ObservableCollection<Brush> Foos { get { return foos; } }
        public Test()
        {
            InitializeComponent();
            Foos.Add(Brushes.Green);
            DataContext = this;
        }

        private void SwitchClick(object sender, EventArgs e)
        {
            Foos[0] = Foos[0] == Brushes.Green ? Brushes.Silver : Brushes.Green;
        }
    }
}

This has been very difficult to track down and has caused me a great deal of pain - but it seems ItemsControls do not behave the way I would expect. It almost seems like a bug in WPF - but being new to WPF I'm erring on the side of it's my fault, not theirs.

To reproduce it is very simple - bind an ItemsControl to an ObservableCollection, and then replace an item in the collection. It's so simple I cannot believe Google doesn't find thousands of people with the same problem.

The code below simply binds an ItemsControl to an ObservableCollection of Brush. Change a brush (by clicking the button), and you get a couple of data errors as the rectangle's brush binding is momentarily of the DataContext of the ItemsControl (!), rather than of the new item. This momentary crash of bindings has caused my application to take over half a second to update when run in the debugger whenever I replace an (immutable, regular CLR object) item in the collection - what am I doing wrong?

<Window x:Class="Dummy.Test"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Test" Height="300" Width="300">
    <Grid>
        <ItemsControl ItemsSource="{Binding Foos}">
            <ItemsControl.ItemTemplate>
                <DataTemplate DataType="{x:Type Brush}">
                    <Rectangle Width="20" Height="20" Fill="{Binding}" />
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
        <Button HorizontalAlignment="Center" VerticalAlignment="Bottom" Click="SwitchClick">Switch</Button>
    </Grid>
</Window>
using System;
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Media;

namespace Dummy
{
    public partial class Test : Window
    {
        private readonly ObservableCollection<Brush> foos = new ObservableCollection<Brush>();
        public ObservableCollection<Brush> Foos { get { return foos; } }
        public Test()
        {
            InitializeComponent();
            Foos.Add(Brushes.Green);
            DataContext = this;
        }

        private void SwitchClick(object sender, EventArgs e)
        {
            Foos[0] = Foos[0] == Brushes.Green ? Brushes.Silver : Brushes.Green;
        }
    }
}

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

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

发布评论

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

评论(1

南汐寒笙箫 2024-12-23 13:04:13

Ahmm 在我使用 .NET 4.0 的设备中尝试并成功后,我认为这是 .NET 3.5 中的问题。如果您的客户坚持在.NET 3.5版本中使用它,建议他们升级到.NET 4.0,这个问题将得到解决。谢谢 :)

Ahmm After trying it in my unit which uses .NET 4.0 and it worked out I think this is a problem in .NET 3.5. If you're clients are insisting to use it in .NET 3.5 Version advice them to upgrade to .NET 4.0 and this problem shall be closed. thanks :)

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