当WPF DataGrid为空时,显示“找不到记录”消息

时间:2013-11-26 14:03:32

标签: c# wpf datagrid wpfdatagrid

我正在尝试在我的WPF Datagrid中显示“找不到记录”消息

我在这里尝试了同一个问题的解决方案: Show "No record found" message on a WPF DataGrid when it's empty

但到目前为止,只有当我的ObservableCollection为空时,我才找到添加空行的方法。 这是我的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>

触发器全部按预期工作。但是没有显示该消息。 当我将Trigger Converter从“intIsLessThan”更改为“intIsEqualOrGreaterThan”时,将显示Message而不是实际的datagrid条目。

所以我想只缺少空的默认行。但是如何添加一个空的默认行?

我希望有人可以帮我解决这个问题。 我现在已经找了两天寻找解决方案......

1 个答案:

答案 0 :(得分:3)

我在我的项目中使用它:

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