0

我无法得到最简单的ItemControl工作想法。我只想ItemsControl用一堆SomeItem.

这是代码隐藏:

using System.Collections.ObjectModel;
using System.Windows;

namespace Hax
{

    public partial class MainWindow : Window
    {

        public class SomeItem
        {
            public string Text { get; set; }
            public SomeItem(string text)
            {
                Text = text;
            }
        }

        public ObservableCollection<SomeItem> Participants
            { get { return m_Participants; } set { m_Participants = value; } }
        private ObservableCollection<SomeItem> m_Participants
            = new ObservableCollection<SomeItem>();

        public MainWindow()
        {
            InitializeComponent();
            Participants.Add(new SomeItem("Hej!"));
            Participants.Add(new SomeItem("Tjenare"));
        }
    }
}

这是 XAML:

<ItemsControl Name="itemsParticipants"
    ItemsSource="{Binding Path=Participants}">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <StackPanel />
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Border BorderBrush="Red" BorderThickness="2">
                <TextBlock Text="{Binding Text}" />
            </Border>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

我究竟做错了什么?

4

2 回答 2

1

ItemsSource 引用了一个名为 MyParticipants 的属性,而它看起来是 Participants 的代码。

检查调试输出视图,您应该会看到错误消息。

于 2010-01-25T13:23:46.600 回答
1

确保您的 datacontext 在您的 xaml 中的某处设置为 RelativeSource Self。如果您可以发布更多您的 xaml,它可能会有所帮助。

<ItemsControl... DataContext="{Binding RelativeSource={RelativeSource Self}}" >
于 2010-01-25T13:25:39.980 回答