1

我试图在 WPF 列表框控件中列出一些数据。这是我第一次在 WPF 中使用 DataTemplate。一切正常,除了没有数据时没有显示“没有要显示的项目”。下面是我的代码。

    <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>

我正在绑定数据源,如下所示。

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

当我将 ListBox 的样式属性设置为 ListStyle 时,它​​会引发错误

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

谁能指出如何使它正确?

提前致谢。

4

1 回答 1

5

我使用了下面的代码,它工作正常我的 xaml 代码

<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 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>
于 2012-10-28T18:11:43.743 回答