简体   繁体   English

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

[英]Binding an ObservableCollection to an ItemsControl - not as simple as it sounds?

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. 这一直很难找到,并给我带来了极大的痛苦-但似乎ItemsControls行为不符合我的期望。 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. 这几乎似乎是WPF中的错误-但对WPF来说,这是我的错,而不是他们的错,这对我来说是错误的。

To reproduce it is very simple - bind an ItemsControl to an ObservableCollection , and then replace an item in the collection. 要重现它非常简单-将ItemsControl绑定到ObservableCollection ,然后替换集合中的项目。 It's so simple I cannot believe Google doesn't find thousands of people with the same problem. 如此简单,我简直不敢相信Google找不到成千上万的人遇到同样的问题。

The code below simply binds an ItemsControl to an ObservableCollection of Brush . 下面的代码仅将ItemsControl绑定到BrushObservableCollection 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. 更改画笔(通过单击按钮),您会遇到一些数据错误,因为矩形的画笔绑定暂时是ItemsControl (!)的DataContext而不是新项的。 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? 每当我替换集合中的(不变的,常规的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;
        }
    }
}

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. Ahmm在我的使用.NET 4.0的单元中尝试完之后,我认为这是.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. 如果您的客户坚持要在.NET 3.5版本中使用它,建议他们升级到.NET 4.0,此问题将被解决。 thanks :) 谢谢 :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM