I tried to use the following EmptyDataTemplate:
http://blogs.telerik.com/blogs/posts/10-02-01/empty_data_template_in_radgridview_for_silverlight_and_wpf.aspx
but it fails to work as expected with RadControls_for_Silverlight 2010_2_0812.
Any ideas on how to get it working again?
Any other ideas about the ArgumentOutOfBoundsException that occurs if gridView.ChildrenOfType<Grid>().Count == 0?
Thank you.
8 Answers, 1 is accepted
I have prepared an updated project for you. Please find it attached. Should work with the new binaries.
Let me know in case you have troubles with it.
All the best,
Pavel Pavlov
the Telerik team
Your example works as-is.
However, in my scenario, I am getting an exception:
System.ArgumentOutOfRangeException was unhandled by user code
Message=Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
StackTrace:
at System.ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument argument, ExceptionResource resource)
at System.ThrowHelper.ThrowArgumentOutOfRangeException()
at System.Collections.Generic.List`1.get_Item(Int32 index)
at NoItemsPanel.EmptyDataTemplateBehavior.AssociatedObject_LayoutUpdated(Object sender, EventArgs e)
at System.Windows.FrameworkElement.OnLayoutUpdated(Object sender, EventArgs e)
at MS.Internal.JoltHelper.RaiseEvent(IntPtr target, UInt32 eventId, IntPtr coreEventArgs, UInt32 eventArgsTypeIndex)
This happens when a new instance of the control with the EmptyDataTemplate is created but is not visible.
For example, using the example project given, simple replace the line in App.xaml.cs:
this.RootVisual = new MainPage();
to
MainPage page = new MainPage();
In my real scenario, I am actually using a RadFrameContainer and dynamically displaying RadPages and the EmptyDataTemplate is on the second RadPage for example, but this simple change in the example project given produces the same unexpected behavior.
Any advice?
Thank you.
I think I have found the problem . I have updated the project on my blog post. You may download the updated project here.
The updated method looks like :
void AssociatedObject_LayoutUpdated(object sender, EventArgs e) { Grid rootGrid = this.AssociatedObject.ChildrenOfType<Grid>().FirstOrDefault(); if (rootGrid != null) { this.AssociatedObject.LayoutUpdated -= new EventHandler(AssociatedObject_LayoutUpdated); this.LoadTemplateIntoGridView(this.AssociatedObject); this.AssociatedObject.Items.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(Items_CollectionChanged); SetVisibility(); } }A small change regarding the access of the child grid was made -
instead of accessing by index the FirstOrDefault(); method is used.
All the best,
Pavel Pavlov
the Telerik team
It works!
I am now trying to figure out how to bind the IsEnabled property of the EmptyDataTemplate with the RadGridView.
Any advice?
Thank you.
At the end of the LoadTemplateIntoGridView method, you may add a line to perform the binding.
Here you have access to the content presenter and may use SetBinding to do whatever you need.
I am not sure what exactly you are trying to achieve , as the DataTemplate does not have IsEnabled property.
If you give me a bit more details on the desired behavior I will gladly provide the code for you .
All the best,
Pavel Pavlov
the Telerik team
In the case where RadGridView.IsEnabled = false and there are no items in its datasource, the EmptyDataTemplate is displayed correctly, but I would have expected it to inherit the .IsEnabled property of the RadGridView such that the Border and the TextBlock in the DataTemplate are grayed out.
Thank you.
You can do that by tweaking the EmptyDataTemplateBehavior.cs a bit. First you should change the type of the contentPresenter variable to ContentControl which has an IsEnabled property. Then you need to setup the binding of IsEnabled in LoadTemplateIntoGridView:
// tie the IsEnabled property of the presenter to IsEnabled of the grid Binding binding = new Binding("IsEnabled"); binding.Source = this.AssociatedObject; contentPresenter.SetBinding(ContentControl.IsEnabledProperty, binding);Once you do that, all controls inside the EmptyDataTemplate will be disabled when the grid is disabled. Unfortunately some controls like TextBlock will not change their visual appearance when disabled. If you would like to have a greyed-out text when RadGridView is disabled you can use another type of control - Label for example which is part of the Silverlight toolkit. This control does not change its visual style by default but you can redefine its template and achieve the desired result:
<local:EmptyDataTemplateBehavior> <local:EmptyDataTemplateBehavior.EmptyDataTemplate> <DataTemplate> <input:Label Content="sdfsdf" > <input:Label.Style> <Style TargetType="input:Label"> <Setter Property="IsTabStop" Value="False"/> <Setter Property="HorizontalContentAlignment" Value="Left"/> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="input:Label"> <Grid> <vsm:VisualStateManager.VisualStateGroups> <vsm:VisualStateGroup x:Name="CommonStates"> <vsm:VisualState x:Name="Normal"/> <!-- add state chane when Disabled--> <vsm:VisualState x:Name="Disabled"> <Storyboard> <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentControl" Storyboard.TargetProperty="Foreground" Duration="0:0:1.5"> <DiscreteObjectKeyFrame KeyTime="0"> <DiscreteObjectKeyFrame.Value> <SolidColorBrush Color="Gray" /> </DiscreteObjectKeyFrame.Value> </DiscreteObjectKeyFrame> </ObjectAnimationUsingKeyFrames> </Storyboard> </vsm:VisualState> </vsm:VisualStateGroup> <vsm:VisualStateGroup x:Name="ValidationStates"> <vsm:VisualState x:Name="Valid"/> <vsm:VisualState x:Name="Invalid"> <Storyboard> <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentControl" Storyboard.TargetProperty="Foreground" Duration="0:0:1.5"> <DiscreteObjectKeyFrame KeyTime="0"> <DiscreteObjectKeyFrame.Value> <SolidColorBrush Color="Red" /> </DiscreteObjectKeyFrame.Value> </DiscreteObjectKeyFrame> </ObjectAnimationUsingKeyFrames> </Storyboard> </vsm:VisualState> </vsm:VisualStateGroup> <vsm:VisualStateGroup x:Name="RequiredStates"> <vsm:VisualState x:Name="NotRequired"/> <vsm:VisualState x:Name="Required"> <Storyboard> <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentControl" Storyboard.TargetProperty="FontWeight" Duration="0"> <DiscreteObjectKeyFrame KeyTime="0" Value="SemiBold"/> </ObjectAnimationUsingKeyFrames> </Storyboard> </vsm:VisualState> </vsm:VisualStateGroup> </vsm:VisualStateManager.VisualStateGroups> <Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Padding="{TemplateBinding Padding}" CornerRadius="2"> <ContentControl x:Name="ContentControl" Foreground="{TemplateBinding Foreground}" Content="{TemplateBinding Content}" ContentTemplate="{TemplateBinding ContentTemplate}" FontWeight="{TemplateBinding FontWeight}" Cursor="{TemplateBinding Cursor}" HorizontalAlignment="{TemplateBinding HorizontalAlignment}" HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}" FontFamily="{TemplateBinding FontFamily}" FontSize="{TemplateBinding FontSize}" FontStretch="{TemplateBinding FontStretch}" VerticalAlignment="{TemplateBinding VerticalAlignment}" VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}" IsTabStop="False" /> </Border> </Grid> </ControlTemplate> </Setter.Value> </Setter> </Style> </input:Label.Style> </input:Label> <!--<TextBlock Text="No results found!" HorizontalAlignment="Center" VerticalAlignment="Center" Visibility="{Binding MessageVisibility}" />--> <!--<Button Content="sdfsd"/>--> </DataTemplate> </local:EmptyDataTemplateBehavior.EmptyDataTemplate>Milan
the Telerik team
Thank you very much! :)
xmlns:vsm="clr-namespace:System.Windows;assembly=System.Windows"