在ListBox中显示默认项

时间:2014-11-06 08:56:26

标签: c# wpf xaml

我正在尝试让ListBox在我添加项目之前显示一个初始值(类似于:“没有添加文件”),但是我收到错误“在使用ItemsSource之前,Items集合必须为空。“

我是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,这是我收到错误的地方,在Items集合中显然必须为空才能分配别的东西:

        DbListBox.ItemsSource = DbCollection;
        this.DataContext = this;  

`

有什么建议吗?

1 个答案:

答案 0 :(得分: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>