XAML 当WPF数据网格为空时,在其上显示“未找到记录”消息

wn9m85ua  于 2023-09-28  发布在  其他
关注(0)|答案(5)|浏览(166)

如果没有可用的记录,我想在数据网格上添加一个TextBlock,在标题下面显示消息“No Record Found”。
考虑所附的图像作为参考。

biswetbf

biswetbf1#

这个问题已经发布很久了。但我想这对别人可能有用。

<Window.Resources>
   <BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />
</Window.Resources>

<DataGrid Name="dgProjects" ItemsSource="{Binding Projects}" AutoGenerateColumns="True" />

<TextBlock Text="Employee has no projects" Visibility="{Binding Items.IsEmpty, Converter={StaticResource BooleanToVisibilityConverter}, ElementName=dgProjects}" />

为了简单起见,我设置了AutoGenerateColumns=“True”。请定义列。这样,当绑定空数据源时,列名将与“空行”消息沿着显示。

wwodge7n

wwodge7n2#

最后我终于找到了路。
1.当网格为空时,在网格上添加默认行
1.创建一个RowDetailTemplate,其中包含一个带有消息“No Record Found”的文本块

<DataGrid.RowDetailsTemplate>
    <DataTemplate>
        <StackPanel>
            <TextBlock Text="No Record Found" Width="400"></TextBlock>
        </StackPanel>
    </DataTemplate>
</DataGrid.RowDetailsTemplate>

1.在datagrid上设置样式

<DataGrid.Style>
    <Style TargetType="DataGrid">
        <Setter Property="RowDetailsVisibilityMode" Value="Collapsed"></Setter>
        <Style.Triggers>
            <DataTrigger Binding="{Binding DataContext.IsRecordExists, 
                                    RelativeSource={RelativeSource Mode=FindAncestor,
                                    AncestorType={x:Type local:MainWindow}}}" Value="false">
                <Setter Property="RowHeight" Value="0"></Setter>
                <Setter Property="RowDetailsVisibilityMode" Value="Visible"></Setter>
            </DataTrigger>
        </Style.Triggers>
    </Style>
</DataGrid.Style>

默认情况下(记录在datagrid上可用)行详细信息模板将折叠。
DataTrigger,检查CLR的属性,如果为false,则显示行详细信息模板。
之所以将rowwheight设置为0来隐藏我们在第一步中添加的默认行。

rseugnpd

rseugnpd3#

1.在stackpanel中添加网格
1.放在datagrid旁边的边框代码之下

<Border HorizontalAlignment="Stretch" VerticalAlignment="Center" 
        BorderThickness="1,0,1,1" BorderBrush="Black" Height="35">
    <Border.Style> 
        <Style TargetType="Border">
            <Setter Property="Visibility" Value="Collapsed" />
            <Style.Triggers>
                <DataTrigger Binding="{Binding YourListName.Count}" Value="0">
                    <Setter Property="Visibility" Value="Visible" />
                </DataTrigger>
            </Style.Triggers>
        </Style> 
    </Border.Style> 
    <TextBlock Text="No record fount" HorizontalAlignment="Center"
               VerticalAlignment="Center" /> 
</Border>

它将根据您的收藏/列表计数显示/隐藏。

c0vxltue

c0vxltue4#

我发现很容易将文本块置于网格中心,并根据行数设置其可见性。我通常使用MVVM,并将可见性绑定到View Model属性:

<Grid>
    <toolkit:DataGrid>
        <toolkit:DataGrid.Columns>
           .
           .
           .
        </toolkit:DataGrid.Columns>
    </toolkit:DataGrid>
    <TextBlock Text="No Records Found" HorizontalAlignment="Center"  VerticalAlignment="Center" Visibility="{Binding EmptyMessageVisibility, Mode=OneWay, FallbackValue=Visible}" />
</Grid>
ecfsfe2w

ecfsfe2w5#

我一直在寻找一个通用的解决方案,看起来与原始控件相同,带有标题。只有一行显示没有数据。
我找到了这个答案:https://stackoverflow.com/a/50233655/
不幸的是,这看起来不一样的正常控制,但从我得到的想法,试图修改原来的模板。
这就是我的版本:
数据:

无数据:

1.如果你不想知道这是如何工作的,只需复制底部的代码
1.将下面的Style添加到App.xaml文件中:

<Application.Resources>

    <Style  TargetType="{x:Type DataGrid}">
        <Style.Triggers>
            <DataTrigger Binding="{Binding HasItems, RelativeSource={RelativeSource Self}}" Value="false">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type DataGrid}">
                        <!-- Content of the Original Template and the Label (Part 4). -->
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </DataTrigger>
        </Style.Triggers>
    </Style>

</Application.Resources>

1.将原始模板插入Style
(To获取原始模板:在设计器中右键单击您的DataGrid并选择“编辑样式->编辑副本”)
1.在Grid内的最后一个元素后添加Label(在ScrollViewerControlTemplate内)

<Label Content="No data..." Grid.Row="1" Grid.Column="1" HorizontalAlignment="Center" />

结果为以下样式:

<Style  TargetType="{x:Type DataGrid}">
    <Style.Triggers>
        <DataTrigger Binding="{Binding HasItems, RelativeSource={RelativeSource Self}}" Value="false">
            <Setter Property="Template">
                <Setter.Value>
                    <!-- <Original Template> -->
                    <ControlTemplate TargetType="{x:Type DataGrid}">
                        <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Padding="{TemplateBinding Padding}" SnapsToDevicePixels="True">
                            <ScrollViewer x:Name="DG_ScrollViewer" Focusable="False">
                                <ScrollViewer.Template>
                                    <ControlTemplate TargetType="{x:Type ScrollViewer}">
                                        <Grid>
                                            <Grid.ColumnDefinitions>
                                                <ColumnDefinition Width="Auto"/>
                                                <ColumnDefinition Width="*"/>
                                                <ColumnDefinition Width="Auto"/>
                                            </Grid.ColumnDefinitions>
                                            <Grid.RowDefinitions>
                                                <RowDefinition Height="Auto"/>
                                                <RowDefinition Height="*"/>
                                                <RowDefinition Height="Auto"/>
                                            </Grid.RowDefinitions>
                                            <Button Command="ApplicationCommands.SelectAll" Focusable="False" Style="{DynamicResource {ComponentResourceKey ResourceId=DataGridSelectAllButtonStyle, TypeInTargetAssembly={x:Type DataGrid}}}" Width="{Binding CellsPanelHorizontalOffset, RelativeSource={RelativeSource FindAncestor, AncestorLevel=1, AncestorType={x:Type DataGrid}}}">
                                                <Button.Visibility>
                                                    <Binding Path="HeadersVisibility" RelativeSource="{RelativeSource FindAncestor, AncestorLevel=1, AncestorType={x:Type DataGrid}}">
                                                        <Binding.ConverterParameter>
                                                            <DataGridHeadersVisibility>All</DataGridHeadersVisibility>
                                                        </Binding.ConverterParameter>
                                                    </Binding>
                                                </Button.Visibility>
                                            </Button>
                                            <DataGridColumnHeadersPresenter x:Name="PART_ColumnHeadersPresenter" Grid.Column="1">
                                                <DataGridColumnHeadersPresenter.Visibility>
                                                    <Binding Path="HeadersVisibility" RelativeSource="{RelativeSource FindAncestor, AncestorLevel=1, AncestorType={x:Type DataGrid}}">
                                                        <Binding.ConverterParameter>
                                                            <DataGridHeadersVisibility>Column</DataGridHeadersVisibility>
                                                        </Binding.ConverterParameter>
                                                    </Binding>
                                                </DataGridColumnHeadersPresenter.Visibility>
                                            </DataGridColumnHeadersPresenter>

                                            <ScrollContentPresenter x:Name="PART_ScrollContentPresenter" CanContentScroll="{TemplateBinding CanContentScroll}" CanHorizontallyScroll="False" Grid.ColumnSpan="2" CanVerticallyScroll="False" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" ContentStringFormat="{TemplateBinding ContentStringFormat}" Grid.Row="1"/>
                                            <ScrollBar x:Name="PART_VerticalScrollBar" Grid.Column="2" Maximum="{TemplateBinding ScrollableHeight}" Orientation="Vertical" Grid.Row="1" Visibility="{TemplateBinding ComputedVerticalScrollBarVisibility}" Value="{Binding VerticalOffset, Mode=OneWay, RelativeSource={RelativeSource TemplatedParent}}" ViewportSize="{TemplateBinding ViewportHeight}"/>
                                            <Grid Grid.Column="1" Grid.Row="2">
                                                <Grid.ColumnDefinitions>
                                                    <ColumnDefinition Width="{Binding NonFrozenColumnsViewportHorizontalOffset, RelativeSource={RelativeSource FindAncestor, AncestorLevel=1, AncestorType={x:Type DataGrid}}}"/>
                                                    <ColumnDefinition Width="*"/>
                                                </Grid.ColumnDefinitions>
                                                <ScrollBar x:Name="PART_HorizontalScrollBar" Grid.Column="1" Maximum="{TemplateBinding ScrollableWidth}" Orientation="Horizontal" Visibility="{TemplateBinding ComputedHorizontalScrollBarVisibility}" Value="{Binding HorizontalOffset, Mode=OneWay, RelativeSource={RelativeSource TemplatedParent}}" ViewportSize="{TemplateBinding ViewportWidth}"/>
                                            </Grid>
                                                    
                                            <!-- This is the only Modification to the original Template -->
                                            <Label Content="No data..." Grid.Row="1" Grid.Column="1" HorizontalAlignment="Center" />
                                            <!-- -->
                                        </Grid>
                                    </ControlTemplate>
                                </ScrollViewer.Template>
                                <ItemsPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                            </ScrollViewer>
                        </Border>
                    </ControlTemplate>
                    <!-- </Original Template> -->
                </Setter.Value>
            </Setter>
        </DataTrigger>
    </Style.Triggers>

</Style>

相关问题