显示“未找到记录"WPF DataGrid 为空时的消息 [英] Show "No record found" message on a WPF DataGrid when it's empty
问题描述
如果没有可用的记录,我想在数据网格的标题下方添加一个 TextBlock
,显示消息未找到记录".
If there is no record available, I want to add a TextBlock
on data grid, below the header, showing the message "No Record Found."
考虑附加的图像以供参考.
Consider the attached image for reference.
推荐答案
终于找到方法了.
- 当网格为空时,在网格上添加一个默认行
创建一个 RowDetailTemplate,其中包含一个带有消息未找到记录"的文本块
- When the grid in empty, add a default row on grid
Create a RowDetailTemplate which contain a text block with a message "No Record Found"
<DataGrid.RowDetailsTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="No Record Found" Width="400"></TextBlock>
</StackPanel>
</DataTemplate>
</DataGrid.RowDetailsTemplate>
设置数据网格的样式
Set the style on 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>
默认情况下(数据网格上可用的记录)行详细信息模板将被折叠.
By default (record available on datagrid) row detail template will be collapsed.
检查 CLR poperty 的 DataTrigger,如果为 false,则显示行详细信息模板.
DataTrigger that checks CLR poperty, if it is false then show the row detail template.
将行高设置为 0 以隐藏我们在第一步添加的默认行的原因.
The reason for setting the rowheight as 0 to hide the default row which we haved added on 1st step.
这篇关于显示“未找到记录"WPF DataGrid 为空时的消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!