简体   繁体   中英

Show “No record found” message on a WPF DataGrid when it's empty

I'm trying to display a "No record found" Message in my WPF Datagrid

I tried the solution from the same question here: Show "No record found" message on a WPF DataGrid when it's empty

But so far I didn't find a way to add a empty line only when my ObservableCollection is empty. This is the code for my datagrid:

    <DataGrid
        DataContext="{Binding RelativeSource={RelativeSource AncestorType=Window}}"
        ItemsSource="{Binding CameraListObjCode}"
        AutoGenerateColumns="False"
        Height="145"
        HorizontalAlignment="Left"
        Margin="62,105,0,0"
        Name="dataGrid1"
        VerticalAlignment="Top"
        Width="361"
        IsReadOnly="True">

        <DataGrid.Resources>
            <conv:IntIsEqualOrGreaterThan x:Key="intIsEqualOrGreaterThan"/>
            <conv:IntIsLessThan x:Key="intIsLessThan"/>

            <Style TargetType="DataGrid">
                <Setter Property="RowDetailsVisibilityMode" Value="Collapsed"></Setter>
                <Style.Triggers>
                    <!--<DataTrigger Binding="{Binding Path=CameraListObjCode.Count, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}, Converter={StaticResource intIsEqualOrGreaterThan}, ConverterParameter=1}" Value="True">-->
                    <DataTrigger Binding="{Binding Path=CameraListObjCode.Count, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}, Converter={StaticResource intIsLessThan}, ConverterParameter=1}" Value="True">
                        <Setter Property="RowHeight" Value="0"></Setter>
                        <Setter Property="RowDetailsVisibilityMode" Value="Visible"></Setter>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </DataGrid.Resources>

        <DataGrid.Columns>
            <DataGridTextColumn Header="Camera Model:" Binding="{Binding CameraModel}" />
            <DataGridTextColumn Header="Serial Nr:" Binding="{Binding SerialNr}" />
            <DataGridTextColumn Header="IP Address:" Binding="{Binding IPAddress}" />
        </DataGrid.Columns>

        <DataGrid.RowDetailsTemplate>
            <DataTemplate>
                <StackPanel>
                    <TextBlock Text="No Record Found" Visibility="Visible" />
                </StackPanel>
            </DataTemplate>
        </DataGrid.RowDetailsTemplate>

    </DataGrid>

The Triggers all work as as expected. But the message is not displayed. When I change the Trigger Converter from "intIsLessThan" to "intIsEqualOrGreaterThan" the Message is shown instead of the actual datagrid entry.

So I guess just the empty default row is missing. But how can I add an empty default row?

I hope anyone can help me with this problem. I'm already searching for two days now to find a solution...

i use this in my project:

<Style x:Key="{x:Type ItemsControl}" TargetType="{x:Type ItemsControl}">
    <Setter Property="Background" Value="Transparent"/>
    <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 record found" 
                                       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 record found" 
                                       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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM