1

I am try to list some data in WPF listbox control. It is the first time I am using DataTemplate in WPF. Everything is working fine except when there is no data it is not showing 'No items to display'. Below is my code.

    <ListBox Name="itemsCtrl" Background="#FFE5E5E5" BorderBrush="{x:Null}" SelectionChanged="itemsCtrl_SelectionChanged" Style="{StaticResource ListStyle}">

        <ListBox.Resources>
            <!-- Set SelectedItem Background here -->
            <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="#C2C2C2"/>
            <Style TargetType="{x:Type ListBoxItem}">
                <Style.Triggers>
                    <DataTrigger Binding="{Binding IsMouseOver,
                                    RelativeSource={RelativeSource
                                      Self}}"
                        Value="True">
                        <Setter Property="Background"
                         Value="#C2C2C2" />
                    </DataTrigger>
                </Style.Triggers>
            </Style>
            <Style TargetType="ListBox" x:Key="ListStyle" BasedOn="{StaticResource {x:Type ListBox}}">
                <Style.Triggers>
                    <DataTrigger 
    Binding="{Binding RelativeSource={x:Static RelativeSource.Self}, Path=Items.Count}" 
    Value="0"
    >
                        <Setter Property="Template">
                            <Setter.Value>
                                <ControlTemplate>
                                    <TextBlock>No items to display</TextBlock>
                                </ControlTemplate>
                            </Setter.Value>
                        </Setter>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </ListBox.Resources>

        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel Cursor="Hand" Name="hoverDataTemplate" Orientation="Horizontal" Width="370" VerticalAlignment="Top" Height="40" HorizontalAlignment="Left" >
                    <Label VerticalContentAlignment="Center" HorizontalAlignment="Left" Padding="15,5,5,5" Width="330"  Content="{Binding Path=EVENT_TITLE}" FontSize="12"/>
                    <Image Height="28" Source="/pr;component/Images/black_next.png" Stretch="Fill" Width="28" />

                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

And I am binding data source as shown below.

    itemsCtrl.ItemsSource = dao.SelectDataTable(cmd).DefaultView;

When I set the style property of ListBox as ListStyle it is throwing an error

'Provide value on 'System.Windows.StaticResourceExtension' threw an exception.' Line number '292' and line position '22'.

Can anybody point out how to make it correct?

Thanks in advance.

5
  • 1
    you've defined ListStyle but you didn't set ListBox's style to it. Commented Oct 28, 2012 at 18:03
  • @Bizz thanks for the comment. I missed to post that line here. I have updated my question. Setting liststyle also is not working. Commented Oct 28, 2012 at 18:31
  • if "Items" is null, then Items.Count won't be equal to 0. make sure you instantiate Items before anything. Commented Oct 28, 2012 at 18:44
  • I checked item is not null. but its throwing an error while setting listbox style. 'Provide value on 'System.Windows.StaticResourceExtension' threw an exception.' Line number '292' and line position '22'. Commented Oct 28, 2012 at 18:47
  • Does this answer your question? Display some text when binded listview has no items Commented May 13, 2020 at 20:11

1 Answer 1

5

i have used below code and it's work fine my xaml code

<ListBox Grid.ColumnSpan="2" Grid.Row="1" Height="32" HorizontalAlignment="Left" Margin="160,2,0,0" Name="listBox1" VerticalAlignment="Top" Width="120" Style="{StaticResource Dhaval}"/>

Style look like

<Style x:Key="Dhaval" TargetType="{x:Type ListBox}">
    <Style.Triggers>
        <DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=Items.Count}" Value="0">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate>
                        <TextBlock>No items to display</TextBlock>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </DataTrigger>
     </Style.Triggers>
</Style>
Sign up to request clarification or add additional context in comments.

3 Comments

have u checked your listbox should have no value when u want to show no items to display textblock? because it's work fine if your listbox should have no value. i chekced it proper.
it is showing an error 'Provide value on 'System.Windows.StaticResourceExtension' threw an exception.' Line number '292' and line position '22'. when I set listbox style.
set the style in App.xaml file and then give StaticResource. i think it should work now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.