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

[Solved] RadGridView.HasItems is false when the Visibility is set to Collapsed

1 Answer 235 Views
GridView
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Geoff Hardy
Top achievements
Rank 1
Geoff Hardy asked on 25 Feb 2010, 10:06 PM
We have a RadGridView that we only want to display whenever it has some rows. When I has no rows, its visibility should be collapsed. However if we bind its visibility to the HasItems property, HasItems always seems to evaulate to false when the Visibility is set to Collapsed. Is this intentional or a bug? Is there a workaround to determine if there are items in a grid, regardless of whether or not it is visible?

1 Answer, 1 is accepted

Sort by
0
Vlad
Telerik team
answered on 26 Feb 2010, 07:33 AM
Hi,

The grid will not be bound if the Visibility is set to Collapsed and that is why HasItems will be false. You can create addition property to your view model to achieve the desired result:

XAML
<UserControl x:Class="SilverlightApplication1.MainPage"
    mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480"
    xmlns:telerik="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls"
    xmlns:telerikGrid="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls.GridView">
    <Grid x:Name="LayoutRoot">
        <Grid.Resources>
            <telerik:BooleanToVisibilityConverter x:Key="converter" />
        </Grid.Resources>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition />
        </Grid.RowDefinitions>
        <Button Content="Clear" Click="Button_Click" />
        <telerikGrid:RadGridView Grid.Row="1" ItemsSource="{Binding Data}"
                                 Visibility="{Binding HasItems, Converter={StaticResource converter}}" />
    </Grid>
</UserControl>


C#
using System;
using System.Linq;
using System.Collections;
using System.ComponentModel;
using System.Windows.Controls;
using System.Collections.Generic;
using System.Collections.ObjectModel;
 
namespace SilverlightApplication1
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
 
            DataContext = new MyDataContext();
        }
 
        private void Button_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            ((MyDataContext)DataContext).ClearData();
        }
    }
 
    public class MyDataContext : INotifyPropertyChanged
    {
        ObservableCollection<MyObject> _Data;
        public ObservableCollection<MyObject> Data
        {
            get
            {
                if (_Data == null)
                {
                    _Data = new ObservableCollection<MyObject>();
                    (from i in Enumerable.Range(0, 100)
                     select new MyObject() { ID = i, Name = String.Format("Name{0}", i) }).ToList().ForEach(_Data.Add);
                }
 
                return _Data;
            }
        }
 
        public void ClearData()
        {
            Data.Clear();
            this.OnPropertyChanged("HasItems");
        }
 
        public bool HasItems
        {
            get
            {
                return Data.Any();
            }
        }
 
        #region INotifyPropertyChanged Members
 
        private void OnPropertyChanged(string propertyName)
        {
            if (this.PropertyChanged != null)
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
 
        public event PropertyChangedEventHandler PropertyChanged;
 
        #endregion
    }
 
    public class MyObject
    {
        public int ID { get; set; }
        public string Name { get; set; }
    }
}



Sincerely yours,
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.
Tags
GridView
Asked by
Geoff Hardy
Top achievements
Rank 1
Answers by
Vlad
Telerik team
Share this question
or