How-to define Empty DataTemplate for the ItemsControl based controls like ListView or DataGrid

后端 未结 3 1344
刺人心
刺人心 2021-02-13 10:31

ASP.NET controls like ListView allows providing a custom template by setting the ListView.EmptyDataTemplate property, this template will be rendered in case of empty data source

3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-02-13 10:51

    you can use DataTemplate selector to do that.

    http://msdn.microsoft.com/en-us/library/system.windows.controls.datatemplateselector.aspx

    UPDATE 1

    Code:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    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 EmptyRowsTemplate
    {
        /// 
        /// Interaction logic for MainWindow.xaml
        /// 
        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
    
                this.Loaded += (o, e) => 
                {
                    this.l.ItemsSource = new List(3)
                    {
                        "A",
                        null,
                        "B"
                    };
                };
            }
        }
    
        public class TemplateManager : DependencyObject
        {
            public static readonly DependencyProperty BlankDataTemplateProperty =
                DependencyProperty.RegisterAttached("BlankDataTemplate",
                typeof(DataTemplate),
                typeof(TemplateManager),
                new PropertyMetadata(new PropertyChangedCallback((o, e) => 
                {
                    ((ItemsControl)o).ItemTemplateSelector = new BlankDataTemplateSelector();
                })));
    
            public static void SetBlankDataTemplate(DependencyObject o, DataTemplate e)
            {
                o.SetValue(TemplateManager.BlankDataTemplateProperty, e);
            }
    
            public static DataTemplate GetBlankDataTemplate(DependencyObject o)
            {
                return (DataTemplate)o.GetValue(TemplateManager.BlankDataTemplateProperty);
            }
    
            private class BlankDataTemplateSelector : DataTemplateSelector
            {
                public BlankDataTemplateSelector()
                    : base()
                {
                }
    
                public override DataTemplate SelectTemplate(object item, DependencyObject container)
                {
                    ItemsControl itemControl =
                        (ItemsControl)ItemsControl.ItemsControlFromItemContainer(ItemsControl.ContainerFromElement(null, container));
    
                    if (item == null)
                    {
                        return TemplateManager.GetBlankDataTemplate(itemControl);
                    }
                    else
                    {
                        return base.SelectTemplate(item, container);
                    }
    
                }
            }
        }
    }
    

    Markup
    

    
        
            
                
                    
                        
                    
                
            
        
    
    

提交回复
热议问题