none
wpf datagrid没有数据时怎样显示“暂无数据”四个大字 RRS feed

  • 问题

  • wpf datagrid没有数据时怎样显示“暂无数据”四个大字

    有绑定数据源,但是数据源的记录是0.

    2017年1月10日 6:52

答案

  • 方法有很多种,转换器可以实现

    class NoDataStringConverter : IValueConverter
        {
            public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
            {
                return ((int)value) == 0 ? "暂无数据":"";
            }

            public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
            {
                throw new NotImplementedException();
            }
        }

    <ListView.Background>
                                <VisualBrush>
                                    <VisualBrush.Visual>
                                        <TextBlock Text="{Binding Items.Count,Converter={StaticResource NoDataStringConverter}, ElementName=listView}" Opacity=".5"/>
                                    </VisualBrush.Visual>
                                    <VisualBrush.RelativeTransform>
                                        <TransformGroup>
                                            <ScaleTransform ScaleX=".1" ScaleY=".1" CenterX=".5" CenterY=".5"/>
                                        </TransformGroup>
                                    </VisualBrush.RelativeTransform>
                                </VisualBrush>
                            </ListView.Background>

    • 已标记为答案 彭123 2017年1月11日 6:24
    2017年1月11日 2:31
  • <Window x:Class="ListView无数据时显示文字.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
            xmlns:local="clr-namespace:ListView无数据时显示文字"
            mc:Ignorable="d"
            Title="MainWindow" Height="350" Width="525">
        <Window.Resources>
            <local:NoDataStringConverter x:Key="NoDataStringConverter"/>
        </Window.Resources>
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
                <RowDefinition/>
            </Grid.RowDefinitions>
            <Button Content="加载、清空" Margin="5" Click="Button_Click"/>
            <ListView Name="lv1" Grid.Row="1">
                <ListView.Background>
                    <VisualBrush>
                        <VisualBrush.Visual>
                            <TextBlock Text="{Binding Items.Count,Converter={StaticResource NoDataStringConverter}, ElementName=lv1}" Opacity=".5"/>
                        </VisualBrush.Visual>
                        <VisualBrush.RelativeTransform>
                            <TransformGroup>
                                <ScaleTransform ScaleX=".1" ScaleY=".1" CenterX=".5" CenterY=".5"/>
                            </TransformGroup>
                        </VisualBrush.RelativeTransform>
                    </VisualBrush>
                </ListView.Background>
                <ListView.View>
                    <GridView>
                        <GridViewColumn Header="id" DisplayMemberBinding="{Binding id}"/>
                        <GridViewColumn Header="value" DisplayMemberBinding="{Binding value}"/>
                        <GridViewColumn Header="value2" DisplayMemberBinding="{Binding value2}"/>
                    </GridView>
                </ListView.View>
            </ListView>
        </Grid>
    </Window>
    

    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Navigation;
    using System.Windows.Shapes;
    
    namespace ListView无数据时显示文字
    {
        /// <summary>
        /// MainWindow.xaml 的交互逻辑
        /// </summary>
        public partial class MainWindow : Window
        {
            DataTable dt = new DataTable();
           
            public MainWindow()
            {
                InitializeComponent();
                dt.Columns.Add("id");
                dt.Columns.Add("value");
                dt.Columns.Add("value2");
                Random rd = new Random();
                for (int i = 0; i < 5; i++)
                {
                    DataRow dr = dt.NewRow();
                    dr[0] = i;
                    dr[1] = rd.Next();
                    dr[2] = rd.Next();
                    dt.Rows.Add(dr);
                }
                lv1.ItemsSource = dt.DefaultView;
            }
    
            private void Button_Click(object sender, RoutedEventArgs e)
            {
                if (lv1.ItemsSource!=null)
                {
                    lv1.ItemsSource = null;
                }
                else
                {
                    lv1.ItemsSource = dt.DefaultView;
                }
            }
        }
    
    }
    

    using System;
    using System.Collections.Generic;
    using System.Globalization;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Data;
    
    namespace ListView无数据时显示文字
    {
        class NoDataStringConverter : IValueConverter
        {
            public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
            {
                return ((int)value) == 0 ? "暂无数据" : "";
            }
    
            public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
            {
                throw new NotImplementedException();
            }
        }
    }
    

    • 已标记为答案 彭123 2017年1月11日 6:25
    2017年1月11日 6:25

  • Hi 彭123,

    >>wpf datagrid没有数据时怎样显示“暂无数据”四个大字

    你也可以参考下面一种实现方式,使用Triggers 来改变背景显示。下面有代码示例。

    <Window.Resources>
            <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 | 暂无数据 " 
                                           />
                                    </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 | 暂无数据 " 
                                           />
                                    </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>
        </Window.Resources>
    
          <DataGrid x:Name="dgvRate" Grid.Row="1" AutoGenerateColumns="True">
                <DataGrid.Columns>
                </DataGrid.Columns>
            </DataGrid>

    Best Regards,

    Yohann Lu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • 已标记为答案 彭123 2017年4月25日 1:42
    2017年1月11日 8:17
    版主

全部回复

  • 使用转换器

    用转换器做不到吧。

    2017年1月11日 1:15
  • 就是类似上图的效果。上图是PHOTOSHOP的效果图。
    2017年1月11日 1:15
  • 判断数据源是否为空,如果空,插入暂无数据

    please verify my account

    2017年1月11日 1:34
  • 判断数据源是否为空,如果空,插入暂无数据

    please verify my account

    你是说,没有数据时,隐藏DATAGRID,然后显示一个TEXTBLOCK控件?
    • 已编辑 彭123 2017年1月11日 1:41
    2017年1月11日 1:41
  • 方法有很多种,转换器可以实现

    class NoDataStringConverter : IValueConverter
        {
            public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
            {
                return ((int)value) == 0 ? "暂无数据":"";
            }

            public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
            {
                throw new NotImplementedException();
            }
        }

    <ListView.Background>
                                <VisualBrush>
                                    <VisualBrush.Visual>
                                        <TextBlock Text="{Binding Items.Count,Converter={StaticResource NoDataStringConverter}, ElementName=listView}" Opacity=".5"/>
                                    </VisualBrush.Visual>
                                    <VisualBrush.RelativeTransform>
                                        <TransformGroup>
                                            <ScaleTransform ScaleX=".1" ScaleY=".1" CenterX=".5" CenterY=".5"/>
                                        </TransformGroup>
                                    </VisualBrush.RelativeTransform>
                                </VisualBrush>
                            </ListView.Background>

    • 已标记为答案 彭123 2017年1月11日 6:24
    2017年1月11日 2:31
  • <Window x:Class="ListView无数据时显示文字.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
            xmlns:local="clr-namespace:ListView无数据时显示文字"
            mc:Ignorable="d"
            Title="MainWindow" Height="350" Width="525">
        <Window.Resources>
            <local:NoDataStringConverter x:Key="NoDataStringConverter"/>
        </Window.Resources>
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
                <RowDefinition/>
            </Grid.RowDefinitions>
            <Button Content="加载、清空" Margin="5" Click="Button_Click"/>
            <ListView Name="lv1" Grid.Row="1">
                <ListView.Background>
                    <VisualBrush>
                        <VisualBrush.Visual>
                            <TextBlock Text="{Binding Items.Count,Converter={StaticResource NoDataStringConverter}, ElementName=lv1}" Opacity=".5"/>
                        </VisualBrush.Visual>
                        <VisualBrush.RelativeTransform>
                            <TransformGroup>
                                <ScaleTransform ScaleX=".1" ScaleY=".1" CenterX=".5" CenterY=".5"/>
                            </TransformGroup>
                        </VisualBrush.RelativeTransform>
                    </VisualBrush>
                </ListView.Background>
                <ListView.View>
                    <GridView>
                        <GridViewColumn Header="id" DisplayMemberBinding="{Binding id}"/>
                        <GridViewColumn Header="value" DisplayMemberBinding="{Binding value}"/>
                        <GridViewColumn Header="value2" DisplayMemberBinding="{Binding value2}"/>
                    </GridView>
                </ListView.View>
            </ListView>
        </Grid>
    </Window>
    

    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Navigation;
    using System.Windows.Shapes;
    
    namespace ListView无数据时显示文字
    {
        /// <summary>
        /// MainWindow.xaml 的交互逻辑
        /// </summary>
        public partial class MainWindow : Window
        {
            DataTable dt = new DataTable();
           
            public MainWindow()
            {
                InitializeComponent();
                dt.Columns.Add("id");
                dt.Columns.Add("value");
                dt.Columns.Add("value2");
                Random rd = new Random();
                for (int i = 0; i < 5; i++)
                {
                    DataRow dr = dt.NewRow();
                    dr[0] = i;
                    dr[1] = rd.Next();
                    dr[2] = rd.Next();
                    dt.Rows.Add(dr);
                }
                lv1.ItemsSource = dt.DefaultView;
            }
    
            private void Button_Click(object sender, RoutedEventArgs e)
            {
                if (lv1.ItemsSource!=null)
                {
                    lv1.ItemsSource = null;
                }
                else
                {
                    lv1.ItemsSource = dt.DefaultView;
                }
            }
        }
    
    }
    

    using System;
    using System.Collections.Generic;
    using System.Globalization;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Data;
    
    namespace ListView无数据时显示文字
    {
        class NoDataStringConverter : IValueConverter
        {
            public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
            {
                return ((int)value) == 0 ? "暂无数据" : "";
            }
    
            public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
            {
                throw new NotImplementedException();
            }
        }
    }
    

    • 已标记为答案 彭123 2017年1月11日 6:25
    2017年1月11日 6:25

  • Hi 彭123,

    >>wpf datagrid没有数据时怎样显示“暂无数据”四个大字

    你也可以参考下面一种实现方式,使用Triggers 来改变背景显示。下面有代码示例。

    <Window.Resources>
            <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 | 暂无数据 " 
                                           />
                                    </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 | 暂无数据 " 
                                           />
                                    </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>
        </Window.Resources>
    
          <DataGrid x:Name="dgvRate" Grid.Row="1" AutoGenerateColumns="True">
                <DataGrid.Columns>
                </DataGrid.Columns>
            </DataGrid>

    Best Regards,

    Yohann Lu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • 已标记为答案 彭123 2017年4月25日 1:42
    2017年1月11日 8:17
    版主