WPF中

时间:2015-06-23 14:10:07

标签: wpf listbox default wpfdatagrid empty-list

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

2 个答案:

答案 0 :(得分:1)

你可以这样做,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>

答案 1 :(得分:0)

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

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