一个列表框的简单的WPF数据绑定为一个字符串的ObservableCollection [英] Simple WPF DataBinding of a ListBox to an ObservableCollection of strings
问题描述
好吧,我只是不明白这一点。请告诉我,为什么我没有得到任何项目在我的列表框(应该是两个字符串空和东西现在):
Okay, I just don't get it. Please tell me why I get no items in my ListBox (should be the two strings "empty" and "stuff" right now ):
XAML:
<Window.DataContext>
<Windows:SettingsWindowModel x:Name="model"/>
</Window.DataContext>
<Window.Resources>
<DataTemplate x:Key="ListItemTemplate">
<ListBoxItem Content="{Binding}" />
</DataTemplate>
</Window.Resources>
<ListBox Name="listBoxActivities" SelectionChanged="ListBoxActivitiesSelectionChanged"
ItemsSource="{Binding Path=IgnoredActivities}"
HorizontalAlignment="Left" VerticalAlignment="Top" MinHeight="40" MinWidth="200"
Padding="5,100,5,0" Height="100" Margin="0,207,0,0" ItemTemplate="{StaticResource ListItemTemplate}" />
在SettingsWindowModel:
In SettingsWindowModel:
private ObservableCollection<String> _ignoredActivities;
public ObservableCollection<String> IgnoredActivities
{
get
{
if (_ignoredActivities == null)
{
// empty
_ignoredActivities = new ObservableCollection<String>() { "empty","stuff" };
}
return _ignoredActivities;
}
}
再说了,你需要知道什么?我是怎么忘了?
Anything more you need to know? What did I forget?
编辑: 也许我应该补充一点的VisualStudio + ReSharper的也没有显示下划线和编译错误。即使不是警告。
Maybe I should add that VisualStudio + ReSharper also show no underlines and compile errors. Not even warnings.
推荐答案
对不起球员,该数据在那里所有的时间。的问题是在视觉细节。填充也得到了施加于子容器列表框(或项目),的因此,项目未坐在列表的顶部。因为我已经但是在列表框的高度,该项目始终低于ListBox的可见高度。施坦格事调试。谢谢您的回答反正!
Sorry guys, the data was there all the time. The problem was in the visual details. The padding also got applied to a sub-container of the ListBox (or the items), therefore the items were not sitting at the top of the list. As I've but a height on the ListBox, the items always were below the visible height of the ListBox. Stange thing to debug. Thanks for your answers anyways!
这篇关于一个列表框的简单的WPF数据绑定为一个字符串的ObservableCollection的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!