【发布时间】:2010-01-25 13:20:10
【问题描述】:
我无法让ItemControl 工作的最简单的想法。我只想用一堆SomeItem 填充我的ItemsControl。
这是代码隐藏:
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>
我做错了什么?
【问题讨论】:
标签: c# wpf datatemplate itemscontrol itemtemplate