EmptyDataTemplate RadGridView

36 posts, 0 answers
  1. Witt
    Witt avatar
    7 posts
    Member since:
    Mar 2015

    Posted 30 Apr 2015 Link to this post

    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

  2. Witt
    Witt avatar
    7 posts
    Member since:
    Mar 2015

    Posted 30 Apr 2015 Link to this post

    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

  3. DevCraft banner
  4. Maya
    Admin
    Maya avatar
    4062 posts

    Posted 30 Apr 2015 Link to this post

    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.

     
  5. Witt
    Witt avatar
    7 posts
    Member since:
    Mar 2015

    Posted 04 May 2015 in reply to Maya Link to this post

    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.

  6. Maya
    Admin
    Maya avatar
    4062 posts

    Posted 06 May 2015 Link to this post

    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.

     
  7. Witt
    Witt avatar
    7 posts
    Member since:
    Mar 2015

    Posted 06 May 2015 in reply to Maya Link to this post

    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.

Back to Top
DevCraft banner