WPF 中空数据网格或列表框的默认内容

Default Content for Empty Datagrid or Listbox in WPF

有谁知道如何将默认内容添加到空的数据网格或列表框?例如 "No Results Returned" 或类似的东西。

                <Style.Triggers>
                    <DataTrigger Binding="{Binding Items.Count, RelativeSource={RelativeSource Self}}" Value="0">
                        <Setter Property="CanUserSortColumns" Value="False" />
                        <Setter Property="Background">
                            <Setter.Value>
                                <VisualBrush Stretch="None">
                                    <VisualBrush.Visual>
                                        <TextBlock Text="We did't find any matching records for your group..." FontSize="14" FontWeight="SemiBold" Foreground="LightCoral"/>
                                    </VisualBrush.Visual>
                                </VisualBrush>
                            </Setter.Value>
                        </Setter>
                    </DataTrigger>
                </Style.Triggers>

这是我发现并能够测试的。感谢任何试图提供帮助的人。

您可以这样做,其中隐藏 ListBox 并显示关联的错误 Grid。这种方法的好处是它更灵活一些,因为您可以使用整个 Grid 而不是 VisualBrush.

<Grid>
    <ListBox x:Name="MyListBox">
        <ListBox.Style>
            <Setter Property="Visibility" Value="Visible" />
            <Style TargetType="ListBox">
                <Style.Triggers>
                    <DataTrigger Binding="{Binding Items.Count, RelativeSource={RelativeSource Self}}" Value="0">
                        <Setter Property="Visibility" Value="Hidden" />
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </ListBox.Style>
    </ListBox>
    <Grid>
        <Grid.Style>
            <Style TargetType="Grid">
                <Setter Property="Visibility" Value="Hidden" />
                <Style.Triggers>
                    <DataTrigger Binding="{Binding ElementName=MyListBox, Path=Items.Count}" Value="0">
                        <Setter Property="Visibility" Value="Visible" />
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        <Grid.Style>
        <TextBlock Text="No Results Returned" />
    </Grid>
<Grid>