【问题标题】:Showing message 'No data' when WPF DataGrid has no rows当 WPF DataGrid 没有行时显示消息“无数据”
【发布时间】:2018-10-18 09:32:51
【问题描述】:

我有一个 WPF DataGrid,当其中没有记录时,我想在其中显示一条消息“无数据”。所以我已经完成了 pcajer 提供的答案中here 的解释,但是当数据网格显示没有数据时,消息不会显示。我想我遇到了AncestorType 的问题,我认为我没有正确指定,但我不知道如何解决这个问题。我完全不明白AncestorType 的工作原理......

在我的代码下面:

<Window x:Class="My.Apps.WPF.Test.wMain"
        xmlns:local="clr-namespace:My.Apps.WPF.Test">

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

<!-- Missatge quan no hi ha documents pel procés seleccionat -->
<dg:DataGrid.RowDetailsTemplate>
    <DataTemplate>
        <StackPanel>
            <TextBlock Text="No hi ha documents disponibles pel procés seleccionat" Width="400"></TextBlock>
        </StackPanel>
    </DataTemplate>
</dg:DataGrid.RowDetailsTemplate>

</Window>

代码隐藏 (XAML.cs):

namespace My.Apps.WPF.Test
{
    public partial class wMain : ViewBaseControl
    {

    }
}

【问题讨论】:

  • 这是 wMain 类中的 IsRecordExists 属性吗?

标签: c# wpf .net-3.5 wpfdatagrid


【解决方案1】:

您可以绑定到DataGridHasItems 属性并更改模板:

<DataGrid.Style>
    <Style TargetType="DataGrid">
        <Style.Triggers>
            <DataTrigger Binding="{Binding HasItems, RelativeSource={RelativeSource Self}}" Value="false">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="DataGrid">
                            <TextBlock Text="No data..." />
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </DataTrigger>
        </Style.Triggers>
    </Style>
</DataGrid.Style>

如果DataGrid 中没有行,则永远不会显示任何行详细信息,因此该方法将不起作用。

编辑:如果你想在没有行的情况下显示列标题,你应该在模板中包含一个DataGridColumnHeadersPresenter

<DataTrigger Binding="{Binding HasItems, RelativeSource={RelativeSource Self}}" Value="false">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="DataGrid">
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="*" />
                    </Grid.RowDefinitions>
                    <DataGridColumnHeadersPresenter x:Name="PART_ColumnHeadersPresenter" Grid.Column="1" Visibility="{Binding HeadersVisibility, ConverterParameter={x:Static DataGridHeadersVisibility.Column}, Converter={x:Static DataGrid.HeadersVisibilityConverter}, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}"/>
                    <TextBlock Text="No data..." Grid.Row="1" />
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</DataTrigger>

【讨论】:

  • 好的,您的解决方案有效,但未显示数据网格列标题。你的例子有可能吗?
  • 那么你需要在模板中包含一个DataGridColumnHeadersPresenter。查看我的编辑。
  • 它显示以下错误:错误 109 'System.Windows.Controls.Grid' 不是 Setter 中属性 'System.Windows.Controls.Control.Template' 的有效值。
  • 根据我的回答,Template 属性应该设置为 ControlTemplate 而没有别的。
  • 我不知道您为什么在示例代码中为 DataGrid 使用 dg 前缀,但是对于 TargetType 来说它是相同的,即:TargetType="dg:DataGrid"
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-31
  • 1970-01-01
  • 2010-11-28
  • 2011-06-08
  • 1970-01-01
相关资源
最近更新 更多