【问题标题】:Can't use list box DataTemplate: Operation is not valid while ItemsSource is in use无法使用列表框 DataTemplate:使用 ItemsSource 时操作无效
【发布时间】:2014-03-19 23:35:35
【问题描述】:

我有一个非常简单的 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>

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

【问题讨论】:

    标签: c# wpf mvvm


    【解决方案1】:

    你忘了ListBox.ItemTemplate标签

    <ListBox>
        <ListBox.ItemTemplate>
            <DataTemplate>
    

    没有 WPF 假设您要定义项目并且您已经绑定 ItemsSource 因此您得到的错误

    【讨论】:

    • 当你在没有任何经验的情况下开始在 WPF 中进行原型设计时会发生这种情况......你问自己:“这是适合这项工作的工具......它应该不会太难学一天之内 :-)" 非常感谢。
    • 很高兴它有帮助。 XAML 其实很容易理解。如果您不能将某些内联内容指定为属性,例如&lt;MyTag MyProperty=...,那么您需要在MyTag 内执行此操作,但是在属性名称中您需要包含专利标签的名称。 &lt;MyTag&gt;&lt;MyTag.MyProperty&gt; 否则,您可以为 [ContentPropertyAttribute("Items")] 属性中指定的默认内容属性和每个 ItemsControl 定义值,正如您在 MSDN 上看到的那样,它是 Items 属性
    猜你喜欢
    • 2018-09-22
    • 2014-05-07
    • 1970-01-01
    • 2016-03-26
    • 2012-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多