2014-11-06 45 views
1

我试图让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; 

`

有什么建议?

+0

你加入'DataTrigger'成'ListBox.ItemsSource'。 – Sinatr 2014-11-06 09:26:25

+0

我现在可以看到。这不会奏效。 – Left4Cookies 2014-11-06 09:45:37

回答

1

您可以尝试使用一些触发(我可以看到你使用的触发,但它无法正常加)听Items.CountItemsSource.CountItemsSource.IsEmptyHasItems属性和设置Template一些TextBlock(有一些文字显示你的消息想要),以便它在ListBox中居中(水平和垂直)。

<ListBox.Style> 
    <Style TargetType="ListBox"> 
     <Style.Triggers> 
     <Trigger Property="HasItems" Value="False"> 
      <Setter Property="Template"> 
       <Setter.Value> 
        <ControlTemplate TargetType="ListBox"> 
        <Border Background="{TemplateBinding Background}"> 
         <TextBlock VerticalAlignment="Center" 
           HorizontalAlignment="Center" 
           Foreground="Gray" FontSize="30" 
           Text="No files added"/> 
        </Border> 
        </ControlTemplate> 
       </Setter.Value> 
      </Setter> 
     </Trigger> 
     </Style.Triggers> 
    </Style> 
</ListBox.Style> 

改变Template在某些情况下,这可能需要你添加更多的代码东西矫枉过正。我们还可以设置Background一些VisualBrush显示来自一个TextBlock捕捉这样一些文字:

<ListBox.Style> 
    <Style TargetType="ListBox"> 
    <Style.Triggers> 
     <Trigger Property="HasItems" Value="False"> 
     <Setter Property="Background"> 
      <Setter.Value>     
       <VisualBrush Stretch="None"> 
       <VisualBrush.Visual> 
        <TextBlock Text="No files added" FontSize="30" 
          Foreground="Gray"/> 
       </VisualBrush.Visual> 
       </VisualBrush> 
      </Setter.Value> 
      </Setter> 
     </Trigger> 
    </Style.Triggers> 
    </Style> 
</ListBox.Style>   
+0

@ Left4Cookies欢迎您,看看我的更新另一个整洁的代码。我认为设置背景比设置模板更好。 – 2014-11-06 09:59:09

+0

所以我们只是设置ListBox的背景,而不是在它上面添加一个controltemplate? – Left4Cookies 2014-11-06 10:19:58

+0

@ Left4Cookies类似的东西,但设置ControlTemplate将完全替代ListBox(不放置在ListBox的顶部)。这就是为什么有时候你可能需要更多的代码,比如当你为ListBox设置一个'BorderBrush'和'BorderThickness'时,你必须将内部'Border'的'BorderBrush'和'BorderThickness'绑定到列表框(我做在第一个代码中可以看到类似于Background的背景:'Background =“{TemplateBinding Background}”')。 – 2014-11-06 10:48:22