不能使用列表框DataTemplate:当ItemsSource正在使用时,操作无效

本文关键字:无效 操作 列表 DataTemplate 不能 ItemsSource | 更新日期: 2023-09-27 17:50:58

我有一个非常简单的XAML:

<Window x:Class="MonteChat.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        DataContext="{Binding RelativeSource={RelativeSource Self}}"
        mc:Ignorable="d" 
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
        d:DesignHeight="300" d:DesignWidth="364" 
        Width="320" Height="480"
        >
    <Grid>
        <Image Height="64" HorizontalAlignment="Left" Margin="12,12,0,0" Name="image1" Stretch="Uniform" VerticalAlignment="Top" Width="64" Source="/MonteChat;component/Resources/Images/person.png" />
        <Label Content="{Binding User.Name}" Height="33" Margin="82,12,12,0" Name="labelUserName" VerticalAlignment="Top" FontWeight="ExtraBold" FontSize="16" ClipToBounds="False" BorderBrush="Black" BorderThickness="1" />
        <ListBox Margin="12,82,12,12" Name="listContacts" ItemsSource="{Binding User.Contacts}">
            <DataTemplate>
                <TextBlock Text="{Binding Name}" />
            </DataTemplate>
        </ListBox>
    </Grid>
</Window>

代码隐藏文件如下所示:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using MonteChat.Model;
namespace MonteChat
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow() {
            var user = new ChatUser();
            user.Name = "Felipe Machado";
            user.AddContact(new ChatUser {
                Name = "Luciana Vicente",
            });
            User = user;
            InitializeComponent();
        }
        public ChatUser User { get; set; }
    }
}

ChatUser类是直接的(我没有使用ViewModel,它没有属性更改通知)。我写这个只是为了一个原型。我将在最终的应用程序中使用MVVM)。

using System;
using System.Collections.Generic;
using System.Linq;
namespace MonteChat.Model
{
    public class ChatUser 
    {
        public virtual Guid Id { get; set; }
        public virtual string Name { get; set;  }
        public virtual string Alias { get; set; }
        public virtual bool Enabled { get; set; }
        public override bool Equals(object obj) {
            return Id.Equals(obj);
        }
        public override int GetHashCode() {
            return Id.GetHashCode();
        }
        public IEnumerable<ChatUser> Contacts {
            get { return contacts; }
        }
        public HashSet<ChatUser> contacts = new HashSet<ChatUser>();
        public bool HasContact(ChatUser contact) {
            return contacts.Contains(contact);
        }
        public void AddContact(ChatUser contact) {
            if (!HasContact(contact))
                contacts.Add(contact);
        }
        public void DeleteContact(ChatUser contact) {
            if (HasContact(contact))
                contacts.Remove(contact);
        }
        public override string ToString() {
            return Name;
        }
    }
}

当我执行应用程序时,我得到这个错误:

{"Operation is not valid while ItemsSource is in use. Access and modify elements with ItemsControl.ItemsSource instead."}

如果我从列表框中删除DataItemTemplate,它可以工作:

<Window x:Class="MonteChat.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        DataContext="{Binding RelativeSource={RelativeSource Self}}"
        mc:Ignorable="d" 
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
        d:DesignHeight="300" d:DesignWidth="364" 
        Width="320" Height="480"
        >
    <Grid>
        <Image Height="64" HorizontalAlignment="Left" Margin="12,12,0,0" Name="image1" Stretch="Uniform" VerticalAlignment="Top" Width="64" Source="/MonteChat;component/Resources/Images/person.png" />
        <Label Content="{Binding User.Name}" Height="33" Margin="82,12,12,0" Name="labelUserName" VerticalAlignment="Top" FontWeight="ExtraBold" FontSize="16" ClipToBounds="False" BorderBrush="Black" BorderThickness="1" />
        <ListBox Margin="12,82,12,12" Name="listContacts" ItemsSource="{Binding User.Contacts}">
        </ListBox>
    </Grid>
</Window>

…但我想样式列表框显示用户图片,添加一些动作按钮等

不能使用列表框DataTemplate:当ItemsSource正在使用时,操作无效

你忘了ListBox.ItemTemplate标签

<ListBox>
    <ListBox.ItemTemplate>
        <DataTemplate>

如果没有这个WPF假设你想定义项目并且你已经绑定了ItemsSource因此你得到的错误是