你所得到的错误大概是:
项目集合必须在使用ItemsSource前空。
有可能没有绑定的问题....你的bigest问题是无效的xaml。
我不确定你想要达到什么目的,但我想你想拥有水平仪Stackpanel
的列表框为ItemsPanel
。
那么就应该是这样的:
<ListBox ... >
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" IsItemsHost="True"/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
</ListBox>
然后,你可能想提供一个ItemTemplate
<ListBox ... >
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" IsItemsHost="True"/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate>
<Border Background="Red" Width="150" Height="100">
<TextBlock Text="{Binding Path=programName}" />
</Border>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
编辑
后您编辑您的问题,看来你有新问题。 仍然...您的XAML不应该工作。如果你使用了你提供的问题。这是无效的。
如果您收到类似的结果:
Namespace.FileInfo
Namespace.FileInfo
Namespace.FileInfo
Namespace.FileInfo
那么你在ItemTemplate中结合工作不正常。确保programName
是公共财产。
您用作绑定绑定源属性的绑定属性必须是您的类的公共属性。显式定义的接口属性不能用于绑定目的,也不能访问没有基本实现的受保护的,私有的,内部的或虚拟的属性。
正如我所说。我的代码工作正常。
UPDATE
List<FileInfo>
应该是ListBox
的DataContext
...这可能是...因为你得到这样的结果。你应该检查的是在FileInfo
类是programName
作为公共财产。
它应该是这样的。
public class FileInfo : ObservableObject
{
private string _programName;
public string programName
{
get{ return this._programName;}
set
{
this._programName = value;
RaisePropertyChanged(() => this.programName);
}
}
}
'但我得到以下错误'什么错误....? – DGibbs 2013-05-13 10:30:17
一个猜测,你从来没有提到的错误,但看看BindingList – 2013-05-13 10:32:58
编辑后。那么......似乎不太可能...在ListBox和/ ListBox标签之间应该有Items ...而不是DataTemplate!另外你正在使用ItemsSource,所以应该没有任何东西。 – 2013-05-13 11:26:02