35 Answers, 1 is accepted
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.
I mean a template that is shown when no data is present for the RadGridView at all.
Thanks
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.
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.
Has this been added already? if so what is the template format?
thanks
milind
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
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(
@
"<DataTemplate xmlns="
"http://schemas.microsoft.com/winfx/2006/xaml/presentation"
"
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
}
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.
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:
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.
Thanks.
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.
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.
Thank you for sharing this with the community?
"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?
How do I fix this so that it works? Thanks for the work on this.
This was done for the Silverlight control. So, it may need changes for WPF.
I do not know.
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."
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.
Would you clarify what is the exact structure of your application ? Do you define RadGridView in a RadTabControl or any other element ?
Maya
the Telerik team
Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>
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);
}
}
}
Would you try the following:
1. Remove the line in the LayoutUpdated event:
this.AssociatedObject.LayoutUpdated -= new EventHandler(AssociatedObject_LayoutUpdated);
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 >>
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.
this code is really helped me a lot for applying emptydatatemplete to multiple radgridview's.
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 ;-)
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 ?
Maya
the Telerik team
Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>
Hi,
I've tried changing this line :
<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"
/>
in order to change display text at runtime. I have create property EmptyText in ViewModel.cs as following :
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
Hi,
I've tried changing this line :
<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"
/>
in order to change display text at runtime. I have create property EmptyText in ViewModel.cs as following :
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
Hi,
I've tried changing this line :
<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"
/>
in order to change display text at runtime. I have create property EmptyText in ViewModel.cs as following :
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
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.
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.
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.
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.