显示“没有找到记录”消息在一个WPF DataGrid中时,它是空的 [英] Show “No record found” message on a WPF DataGrid when it's empty

查看:152
本文介绍了显示“没有找到记录”消息在一个WPF DataGrid中时,它是空的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想显示没有找到记录消息在我的WPF Datagrid的

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

我试图从同样的问题,这里的解决方案:
< A HREF =http://stackoverflow.com/questions/4687414/show-no-record-found-message-on-a-wpf-datagrid-when-its-empty>显示[否记录发现"当它在一个WPF DataGrid的消息空

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

但到目前为止,我没有找到一个方法来添加一个空行,只有当我的ObservableCollection是空的。
这是我的DataGrid中的代码:

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>



触发器作为预期的所有工作。但不显示该消息。
当我从intIsLessThan到intIsEqualOrGreaterThan改变触发转换消息显示实际数据网格条目来代替。

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

推荐答案

我在使用我的项目:

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

这篇关于显示“没有找到记录”消息在一个WPF DataGrid中时,它是空的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆