This is a migrated thread and some comments may be shown as answers.

EmptyDataTemplate RadGridView

35 Answers 527 Views
This is a migrated thread and some comments may be shown as answers.
Paloma
Top achievements
Rank 1
Paloma asked on 22 Apr 2009, 09:29 AM
Hi everybody!!!
Does anybody know if there's an EmptyDataTemplate for the RadGridView for Silverlight?
Thanks

35 Answers, 1 is accepted

Sort by
0
Pavel Pavlov
Telerik team
answered on 23 Apr 2009, 12:00 PM
Hi Paloma,

Do you mean a template that is shown when no data is present for the RadGridView at all or a template for the cell when the value is null ?

All the best,
Pavel Pavlov
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
Paloma
Top achievements
Rank 1
answered on 24 Apr 2009, 05:51 AM
Hi Pavel,
I mean a template that is shown when no data is present for the RadGridView at all.
Thanks
0
Pavel Pavlov
Telerik team
answered on 28 Apr 2009, 07:35 AM
Hello Paloma,

Unfortunately we are missing that feature. I have already talked to my team and we have decided to schedule this  for the Q2 release. Meanwhile I can recommend using external panel placed "over" the RadGridView with your custom content or text. Listening to the ItemsSource collection changes and switching visibility of that panel should do the work .
Let me know if you need further  help. I can  prepare  a sample for you .

Greetings,
Pavel Pavlov
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
Ravindranath Wickramanayake
Top achievements
Rank 1
answered on 16 Feb 2010, 10:12 PM
Could you please show sample code. thanks
0
Vlad
Telerik team
answered on 17 Feb 2010, 06:45 AM
Hi,

You can check this post for more info:
http://blogs.telerik.com/pavelpavlov/posts/10-02-01/empty_data_template_in_radgridview_for_silverlight_and_wpf.aspx

Greetings,
Vlad
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
Milind Raje
Top achievements
Rank 1
answered on 04 Feb 2011, 04:58 PM
hi,
Has this been added already? if so what is the template format?
thanks
milind
0
Vlad
Telerik team
answered on 07 Feb 2011, 08:10 AM
Hi,

This is still not part of the grid - you can use the example from the blog post to achieve your goal. 

Best wishes,
Vlad
the Telerik team
Let us know about your Windows Phone 7 application built with RadControls and we will help you promote it. Learn more>>
0
Dean Wyant
Top achievements
Rank 1
answered on 11 May 2011, 03:23 PM
Here you go - an EmptyDataTemplate Attached property.  (The blog posts a behavior)

I needed to set the template from code behind and I thought it would be great to have it be an Attached Property.
As discussed in http://www.telerik.com/help/silverlight/radtabcontrol-item-templates-and-selectors.html and other topics, a DataTemplate property can be set in code, from a Resource and using Style / Setter.

I added a default template so you can just set EmptyText="Your Text." if you do not need your own template.

To access the properties in your xaml, do not forget to add the reference - something like this
    xmlns:gvp="clr-namespace:Telerik.Windows.Controls.GridView;assembly=MyAssembly"
Then you can do this:
    <telerik:RadGridView ... gvp:RadGridViewProperties.EmptyText="No Matching Data."...
Or you can make your own template in a resource like so:

  <telerik:RadGridView ... gvp:RadGridViewProperties.EmptyDataTemplate="{StaticResource MyEmptyDataTemplate}">
  
<UserControl.Resources>     <DataTemplate x:Key="MyEmptyDataTemplate" >       <TextBlock Text="Custom Empty Data Template" HorizontalAlignment="Center"  VerticalAlignment="Center" />     </DataTemplate>      </UserControl.Resources>

I have not tried to set it in a Style, but I believe the syntax would be:

       <Style TargetType="telerik:RadGridView">
           
<Setter Property="gvp:RadGridViewProperties.EmptyDataTemplate">
               
<Setter.Value>
                  <DataTemplate>
                      <TextBlock Text="Custom Empty Data Template" HorizontalAlignment="Center"  VerticalAlignment="Center" />
                  </DataTemplate>   
               </Setter.Value>
           
</Setter>
       
</Style>

In code behind:
RadGridViewProperties.SetEmptyText(grid, "No Matching Data.");
RadGridViewProperties.SetEmptyDataTemplate(grid, MyEmptyDataTemplate);

RadGridViewProperties.cs

using System;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Markup;
using System.Collections;
using Telerik.Windows.Data;
  
namespace Telerik.Windows.Controls.GridView
{
  public class RadGridViewProperties
  {
    //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    #region EmptyDataTemplateHandler
  
    internal class EmptyDataTemplateHandler
    {
      private RadGridView _grid;
      private ContentPresenter _contentPresenter;
  
      internal EmptyDataTemplateHandler(RadGridView grid)
      {
        _grid = grid;
        Grid rootGrid = _grid.ChildrenOfType<Grid>().FirstOrDefault();
        if (rootGrid != null)
        {
          LoadContentPresenter(rootGrid);
          _grid.Items.CollectionChanged -= OnGridCollectionChanged;
          _grid.Items.CollectionChanged += OnGridCollectionChanged;
          SetVisibility();
        }
      }
  
      internal void Unhook()
      {
        _grid.Items.CollectionChanged -= OnGridCollectionChanged;
      }
  
      private void OnGridCollectionChanged(object sender, 
        System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
      {
        SetVisibility();
      }
  
      private void LoadContentPresenter(Grid rootGrid)
      {
        _contentPresenter = new ContentPresenter();
        _contentPresenter.IsHitTestVisible = false;
        _contentPresenter.SetValue(Grid.RowProperty, 2);
        _contentPresenter.SetValue(Grid.RowSpanProperty, 2);
        _contentPresenter.SetValue(Grid.ColumnSpanProperty, 2);
        _contentPresenter.SetValue(Border.MarginProperty, new Thickness(0, 27, 0, 0));
          rootGrid.Children.Add(_contentPresenter);
      }
  
      private void SetVisibility()
      {
        if (_grid != null)
        {
          if (_contentPresenter != null)
          {
            if (_grid.Items.Count == 0)
            {
              LoadEmptyDataTemplate(); // Always load because it may be changed at any time
              _contentPresenter.Visibility = Visibility.Visible;
            }
            else
              _contentPresenter.Visibility = Visibility.Collapsed;
          }
        }
      }
  
      private void LoadEmptyDataTemplate()
      {
        var template = GetEmptyDataTemplate(_grid);
        if (template == null)
          template = GetDefaultTemplate();
        _contentPresenter.ContentTemplate = template;
      }
  
      private DataTemplate GetDefaultTemplate()
      {
        string text = GetEmptyText(_grid);
        if (String.IsNullOrEmpty(text))
          text = "No Matching Records Found.";
  
        var template = XamlReader.Load(String.Format(
xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml"">
   <TextBlock Text=""{0}"" HorizontalAlignment=""Center""  VerticalAlignment=""Center"" />
</DataTemplate>", text));
        return (DataTemplate)template;
      }
    }
    #endregion EmptyDataTemplateHandler
    //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< 
  
    //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    #region EmptyDataTemplate and EmptyText
  
    /// <summary>
    /// The template to use when there is no data to display in the grid
    /// When not set and EmptyText is set, a default template is used
    /// </summary>
    /// <see cref="EmptyTextProperty"/>
    public static readonly DependencyProperty EmptyDataTemplateProperty =
      DependencyProperty.RegisterAttached("EmptyDataTemplate", typeof(DataTemplate), 
      typeof(RadGridView), new PropertyMetadata(OnEmptyDataTemplateChanged));
  
    #region Accessors
    /// <summary>
    /// The template to use when there is no data to display in the grid
    /// When not set and EmptyText is set, a default template is used
    /// </summary>
    /// <see cref="EmptyTextProperty"/>
    public static DataTemplate GetEmptyDataTemplate(DependencyObject obj)
    {
      return (DataTemplate)obj.GetValue(EmptyDataTemplateProperty);
    }
  
    /// <summary>
    /// The template to use when there is no data to display in the grid
    /// When not set and EmptyText is set, a default template is used
    /// </summary>
    /// <see cref="EmptyTextProperty"/>
    public static void SetEmptyDataTemplate(DependencyObject obj, DataTemplate value)
    {
      obj.SetValue(EmptyDataTemplateProperty, value);
    }
    #endregion Accessors
  
    /// <summary>
    /// The text to show when using the default EmptyDataTemplate.
    /// Default text = "No Matching Records Found."
    /// If EmptyDataTemplate is set, EmptyText is not used.
    /// <see cref="EmptyDataTemplateProperty"/>
    /// </summary>
    public static readonly DependencyProperty EmptyTextProperty =
      DependencyProperty.RegisterAttached("EmptyText", typeof(string), 
      typeof(RadGridView), new PropertyMetadata(OnEmptyTextChanged));
  
    #region Accessors
    /// <summary>
    /// The text to show when using the default EmptyDataTemplate.
    /// Default text = "No Matching Records Found."
    /// If EmptyDataTemplate is set, EmptyText is not used.
    /// <see cref="EmptyDataTemplateProperty"/>
    /// </summary>
    public static string GetEmptyText(DependencyObject obj)
    {
      return (string)obj.GetValue(EmptyTextProperty);
    }
  
    /// <summary>
    /// The text to show when using the default EmptyDataTemplate.
    /// Default text = "No Matching Records Found."
    /// If EmptyDataTemplate is set, EmptyText is not used.
    /// <see cref="EmptyDataTemplateProperty"/>
    /// </summary>
    public static void SetEmptyText(DependencyObject obj, string value)
    {
      obj.SetValue(EmptyTextProperty, value);
    }
    #endregion Accessors
  
    #region EventHandlers
    private static void OnEmptyDataTemplateChanged(DependencyObject d, 
      DependencyPropertyChangedEventArgs e)
    {
      var grid = d as RadGridView;
      if (grid != null)
      {
        if (e.NewValue == null)
          UnhookEmptyDataTemplate(grid);
        else
          HookEmptyDataTemplate(grid);
      }
    }
  
    private static void OnEmptyTextChanged(DependencyObject d, 
      DependencyPropertyChangedEventArgs e)
    
      var grid = d as RadGridView;
      if (grid != null)
      {
        if (String.IsNullOrEmpty((String)e.NewValue))
          UnhookEmptyDataTemplate(grid);
        else
          HookEmptyDataTemplate(grid);
      }
    }
  
    private static void OnEmptyDataTemplateGridLoaded(object sender, EventArgs e)
    {
      if (InitEmptyDataTemplate(sender))
        ((RadGridView)sender).Loaded -= OnEmptyDataTemplateGridLoaded;
    }
  
    private static void OnEmptyDataTemplateGridDataLoaded(object sender, EventArgs e)
    {
      if (InitEmptyDataTemplate(sender))
        ((RadGridView)sender).DataLoaded -= OnEmptyDataTemplateGridDataLoaded;
    }
  
    #endregion EventHandlers
  
    #region Implementation
    /// <summary>
    /// The ContentPresenter to used to hold the EmptyDataTemplate
    /// This is used internally to store/retrieve the ContentPresenter
    /// </summary>
    internal static readonly DependencyProperty EmptyDataTemplateHandlerProperty =
      DependencyProperty.RegisterAttached("EmptyDataTemplateHandler"
      typeof(EmptyDataTemplateHandler), typeof(RadGridView), null);
  
    #region Accessors
    internal static EmptyDataTemplateHandler GetEmptyDataTemplateHandler(DependencyObject obj)
    {
      return (EmptyDataTemplateHandler)obj.GetValue(EmptyDataTemplateHandlerProperty);
    }
  
    internal static void SetEmptyDataTemplateHandler(DependencyObject obj, 
      EmptyDataTemplateHandler value)
    {
      EmptyDataTemplateHandler handler = GetEmptyDataTemplateHandler(obj);
      if (handler != null)
        handler.Unhook();
      obj.SetValue(EmptyDataTemplateHandlerProperty, value);
    }
    #endregion Accessors
  
    private static bool InitEmptyDataTemplate(object sender)
    {
      var grid = sender as RadGridView;
      if (grid != null)
      {
        EmptyDataTemplateHandler handler = new EmptyDataTemplateHandler(grid);
        return true;
      }
      return false;
    }
  
  
    private static void HookEmptyDataTemplate(RadGridView grid)
    { // Attach to Loaded, which will attach to CollectionChanged
      UnhookEmptyDataTemplate(grid); // It does not hurt to unhook before hooking
      grid.Loaded += OnEmptyDataTemplateGridLoaded;
      grid.DataLoaded += OnEmptyDataTemplateGridDataLoaded;
    }
  
    private static void UnhookEmptyDataTemplate(RadGridView grid)
    { // Make sure events are no longer attached
      grid.Loaded -= OnEmptyDataTemplateGridLoaded;
      grid.DataLoaded -= OnEmptyDataTemplateGridDataLoaded;
      SetEmptyDataTemplateHandler(grid, null);
    }
  
    #endregion Implementation
  
    #endregion EmptyDataTemplate and EmptyDataText
    //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< 
  
  } // RadGridViewProperties
  
}
0
Brian Sayatovic
Top achievements
Rank 1
answered on 24 May 2011, 04:33 PM
I tried the original behavior and couldn't get it it work.  Then I tried the attached property one and it works! ...better.  But the empty template shows when there isn't any data AND when there IS data.

My ItemsSource is bound to an ObservableCollection.  My code clears and populates that ObservableCollection when someone changes a dropdown.  My breakpoint inside EmptyDataTemplateSetVisibility is never reached because 'grid' is null.  Up the stack, the 'sender' of the event isn't the RadgridView but a Telerik.Windows.Data.DataItemCollection.
0
Dean Wyant
Top achievements
Rank 1
answered on 24 May 2011, 06:26 PM

OK, if you are using the property, which property are you using: EmptyText or EmptyDataTemplate?
Is it set in xaml or code?
Please paste it so I can see.

The properties are not in the EmptyDataTemplate class, they are statics in RadGridViewProperties.
The EmptyDataTemplate class is for attaching in code without using behaviours - it is not set by properties.

The properties should be much better and they work like so:
According to which property is set, OnEmptyDataTemplateChanged or OnEmptyTextChanged should be called. Within that method, "grid" is the "DependencyObject" passed so it should not be null. If the DependencyObject is a "DataItemCollection", then the property is being set for the wrong object.

If you are using the EmptyDataTemplate class (as it sounds like you are), then you have to set it in code like so:

Somewhere in your code:
        EmptyDataTemplateBehavior EDTB = new EmptyDataTemplateBehavior(grid);

Or:
        EmptyDataTemplateBehavior EDTB = new EmptyDataTemplateBehavior();
EDTB.Attach(grid);

The _grid var will be set directly from what you pass, so it should not be null.

I imagine that your problem may be something I do not see. More details would be needed to track it down.

0
Dean Wyant
Top achievements
Rank 1
answered on 24 May 2011, 06:36 PM
Never mind, I see the bug. I am looking into it.
Thanks.
0
Brian Sayatovic
Top achievements
Rank 1
answered on 24 May 2011, 06:37 PM
Hey, no problem.  I appreciate your help!

As far as I can tell, when the collection changes, the sender isn't the RadGridView but the data collection.  I looked but didn't find a way back to the owning RadGridView from the data collection sender.  I was considering tinkering by using a closure around the grid when attaching the event handlers.
0
Dean Wyant
Top achievements
Rank 1
answered on 24 May 2011, 07:40 PM
I updated the post with the new code. I used a class as a dependency property to hold the grid instance and some of the underlying code.
I do not know how to use a closure in this instance. Perhaps the code using a closure would be better?
I kind of like the code with the class because it gets rid of a bunch of static methods.

I hope this version is bug free... no dangling event handlers, etc. .... no guarantees :-)
This was put together pretty hastily, so if anyone has bugs, improvements, please post them.
Thanks.
0
Brian Sayatovic
Top achievements
Rank 1
answered on 25 May 2011, 05:19 PM
This is working great for me.  FWIW, I think closures would've had the same opportunity for memory leaks as the class-based approach you've used.  I think your class-based approach is more understandable though.

Thank you for sharing this with the community?
0
Brian Sayatovic
Top achievements
Rank 1
answered on 25 May 2011, 05:20 PM
Pavel Pavlov said in April 2009 that this was being scheduled for the [2009] Q2 release:

"Unfortunately we are missing that feature. I have already talked to my team and we have decided to schedule this  for the Q2 release."

Whatever happened to that?
0
Rayne
Top achievements
Rank 1
answered on 11 Aug 2011, 03:17 PM
Having trouble with this attached property. Trying to add this to my WPF RadGridView, but when I copied the code I get an error at the XamlReader.Load method. saying "unknown method Load(string).

How do I fix this so that it works? Thanks for the work on this.
0
Dean Wyant
Top achievements
Rank 1
answered on 11 Aug 2011, 03:44 PM
I think WPF XamlReader.Parse(string) is the equivalent of Silverlight XamlReader.Load(string).

This was done for the Silverlight control. So, it may need changes for WPF.
I do not know.

0
Rayne
Top achievements
Rank 1
answered on 11 Aug 2011, 04:32 PM
Thanks. That worked.
0
Robert
Top achievements
Rank 1
answered on 16 Sep 2011, 01:22 PM
Again, what happend to this?

Pavel Pavlov said in April 2009 that this was being scheduled for the [2009] Q2 release:

"Unfortunately we are missing that feature. I have already talked to my team and we have decided to schedule this  for the Q2 release."
0
UGH!!
Top achievements
Rank 1
answered on 20 Sep 2011, 08:20 PM
This feature would be nice to have!!
0
Jonx
Top achievements
Rank 2
answered on 05 Oct 2011, 12:54 AM
Hi,
The problem I have with that behavior is that the line 

Grid rootGrid = gridView.ChildrenOfType<Grid>().FirstOrDefault();

does never return anything... I can see in the control tree using silverlight spy that some grid controls are present in the childrens but the method never returns anything. I tried other types two. Using the latest version 920...

any hints on that?
thanks,
John.
0
Maya
Telerik team
answered on 05 Oct 2011, 07:32 AM
Hello John,

Would you clarify what is the exact structure of your application ? Do you define RadGridView in a RadTabControl or any other element ?
 

Best wishes,
Maya
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

0
Jonx
Top achievements
Rank 2
answered on 05 Oct 2011, 09:23 AM
Hi Maya,
Thank you very much for looking into this...

This is what I have...

<UserControl x:Class="SoPratic.SL.Views.CommandesPage">
    <UserControl.Resources>
        <app:CommandesViewModel x:Key="commandesVM"/>
        <ctls:CommandGridStyleSelector x:Key="CellStyleSelector" />
    </UserControl.Resources>
    <Grid x:Name="LayoutRoot" DataContext="{StaticResource commandesVM}">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="400*" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
        <sdk:Label Grid.Row="0" Content="{Binding CommandDescription, Mode=TwoWay}" HorizontalAlignment="Left" VerticalAlignment="Center" Name="lblOrder" Margin="5,0,0,0" />
        <telerik:RadBusyIndicator Grid.Row="1" Name="busyIndicator" IsBusy="{Binding IsBusy}"
                            BusyContent="Veuillez patienter..." >
            <telerik:RadGridView Name="grdBeneficiaire" IsReadOnly="False" AutoGenerateColumns="False" ShowInsertRow="False"
                                 ItemsSource="{Binding View}" ShowGroupFooters="True" ShowColumnFooters="True" ElementExporting="grdBeneficiaire_ElementExporting"
                                 CanUserInsertRows="False" CanUserDeleteRows="False" RowStyleSelector="{StaticResource CellStyleSelector}"
                                 ScrollViewer.HorizontalScrollBarVisibility="Visible" ScrollViewer.VerticalScrollBarVisibility="Visible" >
                <i:Interaction.Behaviors>
                    <app:EmptyDataTemplateBehavior>
                        <app:EmptyDataTemplateBehavior.EmptyDataTemplate>
                            <DataTemplate>
                                <TextBlock Text="Aucune commande en cours de saisie." Foreground="Gray" HorizontalAlignment="Center"  VerticalAlignment="Center" />
                            </DataTemplate>
                        </app:EmptyDataTemplateBehavior.EmptyDataTemplate>
                    </app:EmptyDataTemplateBehavior>
                </i:Interaction.Behaviors>
                <telerik:RadGridView.GroupRowStyle >
                    <Style TargetType="telerik:GridViewGroupRow">
                        <Setter Property="ShowHeaderAggregates" Value="True" />
                    </Style>
                </telerik:RadGridView.GroupRowStyle>
                <telerik:RadGridView.Columns>
                    <telerik:GridViewDataColumn Header="OdlID" DataMemberBinding="{Binding OdlID}" UniqueName="OdlID" />

And this is my EmptyDataTemplateBehavior:
public class EmptyDataTemplateBehavior : Behavior<RadGridView>
    {
        ContentPresenter contentPresenter = new ContentPresenter();
 
        protected override void OnAttached()
        {
            base.OnAttached();
            this.AssociatedObject.LayoutUpdated += new EventHandler(AssociatedObject_LayoutUpdated);
        }
 
        public DataTemplate EmptyDataTemplate { get; set; }
 
        void AssociatedObject_LayoutUpdated(object sender, EventArgs e)
        {
            this.AssociatedObject.LayoutUpdated -= new EventHandler(AssociatedObject_LayoutUpdated);
            this.LoadTemplateIntoGridView(this.AssociatedObject);
            this.AssociatedObject.Items.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(Items_CollectionChanged);
            SetVisibility();
        }
 
        void Items_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            SetVisibility();
        }
 
        private void SetVisibility()
        {
            if (this.AssociatedObject.Items.Count ==0)
                this.contentPresenter.Visibility = Visibility.Visible;
            else
                this.contentPresenter.Visibility = Visibility.Collapsed;
        }
 
        private void LoadTemplateIntoGridView(RadGridView gridView)
        {
            contentPresenter.IsHitTestVisible = false;
            contentPresenter.DataContext = this;
            contentPresenter.ContentTemplate = this.EmptyDataTemplate;
             
            //TODO make this work
            Grid rootGrid = gridView.ChildrenOfType<Grid>().FirstOrDefault();
            if (rootGrid != null)
            {
                contentPresenter.SetValue(Grid.RowProperty, 2);
                contentPresenter.SetValue(Grid.RowSpanProperty, 2);
                contentPresenter.SetValue(Grid.ColumnSpanProperty, 2);
                contentPresenter.SetValue(Border.MarginProperty, new Thickness(0, 27, 0, 0));
 
                rootGrid.Children.Add(contentPresenter);
            }
        }
    }

0
Maya
Telerik team
answered on 05 Oct 2011, 03:57 PM
Hi John,

Would you try the following:
1. Remove the line in the LayoutUpdated event:

this.AssociatedObject.LayoutUpdated -= new EventHandler(AssociatedObject_LayoutUpdated);
 2. and place it here:
private void LoadTemplateIntoGridView(RadGridView gridView)
        {
  
            contentPresenter.IsHitTestVisible = false;
            contentPresenter.DataContext = this;
            contentPresenter.ContentTemplate = EmptyDataTemplate;
  
            if (gridView.ChildrenOfType<Grid>().Count > 0)
            {
                Grid rootGrid = gridView.ChildrenOfType<Grid>()[1];
                contentPresenter.SetValue(Grid.RowProperty, 2);
                contentPresenter.SetValue(Grid.RowSpanProperty, 2);
                contentPresenter.SetValue(Grid.ColumnSpanProperty, 2);
                contentPresenter.SetValue(FrameworkElement.MarginProperty, new Thickness(0, 27, 0, 0));
                rootGrid.Children.Add(contentPresenter);
                this.AssociatedObject.LayoutUpdated -= new EventHandler(AssociatedObject_LayoutUpdated);
            }
        }

Is that approach resolving the issue ? 

Regards,
Maya
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

0
Jonx
Top achievements
Rank 2
answered on 05 Oct 2011, 04:26 PM
Excellent!
Yes, indeed it works... Very good job...

I double checked here:
http://blogs.telerik.com/blogs/posts/10-02-01/empty_data_template_in_radgridview_for_silverlight_and_wpf.aspx
and the event is not removed, that's why it's working.
I don't know where taht line comes from.

Certainely my bad.

Thanks a lot Maya, I would have been unable to solve this by myself.

John.
0
sandy
Top achievements
Rank 1
Veteran
answered on 27 Jan 2012, 08:28 PM
Excellent.
this code is really helped me a lot for applying emptydatatemplete to multiple radgridview's.
0
ATeam
Top achievements
Rank 1
answered on 27 Jun 2012, 07:22 AM
Hello!

I've tried the EmptyDataTemplate and also midified it but in our scenario it didn't work as expected.

The content of the ContentPresenter, in my case a textblock will not be shown. But if i use a Spy-Tool i see that it is in the rootGrid Container but it seems that it is behind the GridViewVirtualizingPanel.

Is there any chance to bring the EmptyDataTemplate in front of it?

//Update: Setting the foreground property not equal to the background property helps ;-)
0
Maya
Telerik team
answered on 27 Jun 2012, 07:32 AM
Hi Dominik,

Can you try changing the foreground of the TextBlock ? Do you still not see the text ? Could you clarify which version of RadControls you work with ?  

All the best,
Maya
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

0
Witt
Top achievements
Rank 1
answered on 30 Apr 2015, 06:27 AM

Hi,

I've tried changing this line :

<TextBlock Text="Aucune commande en cours de saisie."Foreground="Gray" HorizontalAlignment="Center"  VerticalAlignment="Center" />
<TextBlock Text="Aucune commande en cours de saisie."Foreground="Gray" HorizontalAlignment="Center"  VerticalAlignment="Center" />

to :

<TextBlock Text="{Binding EmptyText}"Foreground="Gray" HorizontalAlignment="Center"  VerticalAlignment="Center" />
<TextBlock Text="{Binding EmptyText}"Foreground="Gray" HorizontalAlignment="Center"  VerticalAlignment="Center" />

 in order to change display text at runtime. I have create property EmptyText in ViewModel.cs as following :

 

 private string _emptyText;
        public string EmptyText
        {
            get
            {
                return _emptyText;
            }
            set
            {
                if (_emptyText != value)
                {
                    _emptyText = value;
                    NotifyOfPropertyChange(() => EmptyText);
                }
            }
        }

The problem is the text doesn't show up. May I have any suggestion or a way to resolve this problem please?

Thanks,
Witt

0
Witt
Top achievements
Rank 1
answered on 30 Apr 2015, 06:27 AM

Hi,

I've tried changing this line :

<TextBlock Text="Aucune commande en cours de saisie."Foreground="Gray" HorizontalAlignment="Center"  VerticalAlignment="Center" />
<TextBlock Text="Aucune commande en cours de saisie."Foreground="Gray" HorizontalAlignment="Center"  VerticalAlignment="Center" />

to :

<TextBlock Text="{Binding EmptyText}"Foreground="Gray" HorizontalAlignment="Center"  VerticalAlignment="Center" />
<TextBlock Text="{Binding EmptyText}"Foreground="Gray" HorizontalAlignment="Center"  VerticalAlignment="Center" />

 in order to change display text at runtime. I have create property EmptyText in ViewModel.cs as following :

 

 private string _emptyText;
        public string EmptyText
        {
            get
            {
                return _emptyText;
            }
            set
            {
                if (_emptyText != value)
                {
                    _emptyText = value;
                    NotifyOfPropertyChange(() => EmptyText);
                }
            }
        }

The problem is the text doesn't show up. May I have any suggestion or a way to resolve this problem please?

Thanks,
Witt

0
Witt
Top achievements
Rank 1
answered on 30 Apr 2015, 06:27 AM

Hi,

I've tried changing this line :

<TextBlock Text="Aucune commande en cours de saisie."Foreground="Gray" HorizontalAlignment="Center"  VerticalAlignment="Center" />
<TextBlock Text="Aucune commande en cours de saisie."Foreground="Gray" HorizontalAlignment="Center"  VerticalAlignment="Center" />

to :

<TextBlock Text="{Binding EmptyText}"Foreground="Gray" HorizontalAlignment="Center"  VerticalAlignment="Center" />
<TextBlock Text="{Binding EmptyText}"Foreground="Gray" HorizontalAlignment="Center"  VerticalAlignment="Center" />

 in order to change display text at runtime. I have create property EmptyText in ViewModel.cs as following :

 

 private string _emptyText;
        public string EmptyText
        {
            get
            {
                return _emptyText;
            }
            set
            {
                if (_emptyText != value)
                {
                    _emptyText = value;
                    NotifyOfPropertyChange(() => EmptyText);
                }
            }
        }

The problem is the text doesn't show up. May I have any suggestion or a way to resolve this problem please?

Thanks,
Witt

0
Maya
Telerik team
answered on 30 Apr 2015, 12:51 PM
Hi Witt,

Could you send us an update project illustrating the behavior you described ? I will investigate it on my side and suggest any further. 


Regards,
Maya
Telerik
 

See What's Next in App Development. Register for TelerikNEXT.

 
0
Witt
Top achievements
Rank 1
answered on 04 May 2015, 11:04 AM

Hi Maya,

Sorry for late response.
This is my code on GridView 

 

<telerik:RadGridView Name="MainDataGrid"
                            Grid.Column="0" Grid.Row="0"
                            ItemsSource="{Binding DataList}"
                            RowStyleSelector="{StaticResource DataGridDuplicateEntityStyleSelector}"
                            Style="{StaticResource CustomRadGridViewStyle}"
                            behavior:DataGridViewColumnsBindingBehavior.Columns="{Binding Columns}"
                            IsFilteringAllowed="{Binding IsFilterAllowed}"
                            SelectedItem="{Binding SelectedItem,Mode=TwoWay}"
                            SelectionMode="Multiple"
                            SelectionUnit="FullRow"
                            >
           <i:Interaction.Behaviors>
               <behavior:MultiSelectBehavior SelectedItems="{Binding SelectedItems}" />
               <behavior:EmptyDataTemplateBehavior>
                   <behavior:EmptyDataTemplateBehavior.EmptyDataTemplate>
                       <DataTemplate>
                           <TextBlock Text="{x:Static res:Label.NoRecordFound}" Foreground="Gray" HorizontalAlignment="Left" VerticalAlignment="Top" Visibility="{Binding MessageVisibility}" Margin="20,10,0,0"/>
                       </DataTemplate>
                   </behavior:EmptyDataTemplateBehavior.EmptyDataTemplate>
               </behavior:EmptyDataTemplateBehavior>
           </i:Interaction.Behaviors>
 
       </telerik:RadGridView>

 

 Behavior :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Interactivity;
using Telerik.Windows.Controls;
 
namespace BWC.UI.WPF.Resources.Behaviors
{
    class EmptyDataTemplateBehavior : Behavior<RadGridView>
    {
        ContentPresenter contentPresenter = new ContentPresenter();
 
        protected override void OnAttached()
        {
            base.OnAttached();
            this.AssociatedObject.LayoutUpdated += new EventHandler(AssociatedObject_LayoutUpdated);
        }
 
        public DataTemplate EmptyDataTemplate { get; set; }
 
        void AssociatedObject_LayoutUpdated(object sender, EventArgs e)
        {
            this.LoadTemplateIntoGridView(this.AssociatedObject);
            this.AssociatedObject.Items.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(Items_CollectionChanged);
            SetVisibility();
        }
 
        void Items_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            SetVisibility();
        }
 
        private void SetVisibility()
        {
            if (this.AssociatedObject.Items.Count == 0)
                this.contentPresenter.Visibility = Visibility.Visible;
            else
                this.contentPresenter.Visibility = Visibility.Collapsed;
        }
 
           private void LoadTemplateIntoGridView(RadGridView gridView)
        {
   
            contentPresenter.IsHitTestVisible = false;
            contentPresenter.DataContext = this;
            contentPresenter.ContentTemplate = EmptyDataTemplate;
   
            if (gridView.ChildrenOfType<Grid>().Count() > 0)
            {
                Grid rootGrid = gridView.ChildrenOfType<Grid>().FirstOrDefault();
                contentPresenter.SetValue(Grid.RowProperty, 2);
                contentPresenter.SetValue(Grid.RowSpanProperty, 2);
                contentPresenter.SetValue(Grid.ColumnSpanProperty, 2);
                contentPresenter.SetValue(FrameworkElement.MarginProperty, new Thickness(0, 27, 0, 0));
                rootGrid.Children.Add(contentPresenter);
                this.AssociatedObject.LayoutUpdated -= new EventHandler(AssociatedObject_LayoutUpdated);
            }
         
        }
    }
}

 

And this is my ViewModel 

     private string _emptyText;
        public string EmptyText
        {
            get
            {
                return _emptyText;
            }
            set
            {
                if (_emptyText != value)
                {
                    _emptyText = value;
 
                    NotifyOfPropertyChange(() => EmptyText);
                }
            }
        }
 
.
.
.
.
        public void InitializeWithBindingData(DataGridModel dataGridModel, ICustomListService customListService)
        {
            RowStyleSelector = new DataGridDuplicateEntityStyleSelector();
            _ICustomListService = customListService;
            IsFilterColumnVisible = dataGridModel.IsFilterColumnVisible;
            IsDetailsColumnVisible = dataGridModel.IsDetailsColumnVisible;
            IsSelectorColumnVisible = dataGridModel.IsSelectorColumnVisible;
            EmptyText = dataGridModel.EmptyText;
 
            BindData(dataGridModel);
        }
 

Thank you for your help, I'm very appreciate :)

Regards,
Witt.

0
Maya
Telerik team
answered on 06 May 2015, 06:45 AM
Hello Witt,

You need to specify the Source of the binding of the TextBlock. I attached a sample project illustrating the approach.

Regards,
Maya
Telerik
 

See What's Next in App Development. Register for TelerikNEXT.

 
0
Witt
Top achievements
Rank 1
answered on 06 May 2015, 08:26 AM

Hi Maya,

Thank you for your solution, it's really look good. Unfortunately, my DataGridViewModel is extending other class that has no parameter-less constructor. So I can not declare it in UserControl.Resource.

By the way, I have found other solution by using relative source as below :

<TextBlock Text="{Binding Path=DataContext.EmptyText ,RelativeSource={RelativeSource Mode=FindAncestor,
                                                   AncestorType=UserControl}}" Foreground="Gray" HorizontalAlignment="Left" VerticalAlignment="Top" Visibility="{Binding MessageVisibility}" Margin="20,10,0,0"/>

Sincerely thank you again for helping :)

Regards,
Witt.

Asked by
Paloma
Top achievements
Rank 1
Answers by
Pavel Pavlov
Telerik team
Paloma
Top achievements
Rank 1
Ravindranath Wickramanayake
Top achievements
Rank 1
Vlad
Telerik team
Milind Raje
Top achievements
Rank 1
Dean Wyant
Top achievements
Rank 1
Brian Sayatovic
Top achievements
Rank 1
Rayne
Top achievements
Rank 1
Robert
Top achievements
Rank 1
UGH!!
Top achievements
Rank 1
Jonx
Top achievements
Rank 2
Maya
Telerik team
sandy
Top achievements
Rank 1
Veteran
ATeam
Top achievements
Rank 1
Witt
Top achievements
Rank 1
Share this question
or