How to Display “No Items found” in a Datagrid

前端 未结 2 624
轮回少年
轮回少年 2021-01-27 03:00
               
                        
                        

        
相关标签:
2条回答
  • 2021-01-27 03:18

    if you just want to display a message in your datagrid when there are no items - you can do this with a style. Put this style in your App.xaml Resources or at least in your datagrid Resources.

        <Style x:Key="{x:Type ItemsControl}" TargetType="{x:Type ItemsControl}">
        <Style.Triggers>
            <DataTrigger Binding="{Binding Items.Count, RelativeSource={RelativeSource Self}}" Value="0">
                <Setter Property="Background">
                    <Setter.Value>
                        <VisualBrush Stretch="None">
                            <VisualBrush.Visual>
                                <TextBlock Text="no items" 
                                           FontFamily="{StaticResource FontFamily}"
                                           FontSize="{StaticResource FontSize}"/>
                            </VisualBrush.Visual>
                        </VisualBrush>
                    </Setter.Value>
                </Setter>
            </DataTrigger>
            <DataTrigger Binding="{Binding Items, RelativeSource={RelativeSource Self}}" Value="{x:Null}">
                <Setter Property="Background">
                    <Setter.Value>
                        <VisualBrush Stretch="None">
                            <VisualBrush.Visual>
                                <TextBlock Text="no items" 
                                           FontFamily="{StaticResource FontFamily}"
                                           FontSize="{StaticResource FontSize}"/>
                            </VisualBrush.Visual>
                        </VisualBrush>
                    </Setter.Value>
                </Setter>
            </DataTrigger>
        </Style.Triggers>
    </Style>
    <Style x:Key="{x:Type DataGrid}" TargetType="{x:Type DataGrid}" BasedOn="{StaticResource {x:Type ItemsControl}}">
    </Style>
    
    0 讨论(0)
  • 2021-01-27 03:19

    Check output window. WPF dumps binding error/exception occurred to output windows. There you gets the chance to verify if it is binding to right source, if path is correct etc.

    You can also try adding dummy converter in the binding expression. Converter provides you chance to debug binding expression

    0 讨论(0)
提交回复
热议问题