我试图让ListBox在我添加项目之前显示一个初始值(如:“没有文件添加”),但我收到错误“Items collection must在使用ItemsSource之前是空的。“在列表框中显示默认项目
我是新来的WPF,我不确定从这里走哪个方向。我已经看过INotifyPropertyChanged interace,但它看起来像一个简单问题的过度复杂的解决方案(或者我误解使用INotifyPropertychanged?)。
我的XAML看起来像这样:
<ListBox ItemsSource="{Binding}"
Name="DbListBox"
Grid.Column="3"
HorizontalAlignment="Left"
Height="246"
Margin="0,99,0,0"
Grid.Row="1"
VerticalAlignment="Top"
Width="211"
SelectionMode="Single"
SelectedItem="{Binding Path=selectedDB,Mode=TwoWay}"
AllowDrop="True"
Drop="DbListBox_Drop">
<DataTrigger Binding="{Binding RelativeSource={x:Static RelativeSource.Self}, Path=Items.Count}" Value="0">
<Setter Property="ListBox.DataContext">
<Setter.Value>
<ControlTemplate>
<TextBlock>Drag .db file here or add below</TextBlock>
</ControlTemplate>
</Setter.Value>
</Setter>
</DataTrigger>
</ListBox>
在我的代码隐藏我有这样的ObservableCollection public ObservableCollection<string> DbCollection;
这我设置为ListBox的的ItemsSource这是我遇到了错误,在项目收集显然必须是空的之前,我可以分配别的东西吧:
DbListBox.ItemsSource = DbCollection;
this.DataContext = this;
`
有什么建议?
你加入'DataTrigger'成'ListBox.ItemsSource'。 – Sinatr 2014-11-06 09:26:25
我现在可以看到。这不会奏效。 – Left4Cookies 2014-11-06 09:45:37