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

时间:2014-03-19 23:35:35

标签: c# wpf mvvm

我有一个非常简单的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,它也没有改变通知。我只是为原型写这个。我&#39; ll在最终的应用程序中使用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>

...但我希望设置列表框样式以显示用户图片,添加一些操作按钮等。

1 个答案:

答案 0 :(得分:2)

您忘记了ListBox.ItemTemplate代码

<ListBox>
    <ListBox.ItemTemplate>
        <DataTemplate>

如果没有WPF假设你想要定义项目而你已经绑定了ItemsSource因此你得到的错误