0
votes

I have a custom datagrid in my application that when its source is empty it should display a message saying "No results", and when the source is not empty it should display the source data and no message should be displayed.

My custom datagrid is:

namespace GenericControls
{
    [TemplatePart(Name = "EmptyDataGridTextBlock", Type = typeof(TextBlock))]
    public class CustomDataGrid : DataGrid
    {
        private TextBlock _txtEmptyDataGrid = null;

        public static readonly DependencyProperty IsEmptyDataGridProperty = DependencyProperty.Register("IsEmptyDataGrid", typeof(bool), typeof(CustomDataGrid), null);
        public static readonly DependencyProperty EmptyDataGridTextProperty = DependencyProperty.Register("EmptyDataGridText", typeof(string), typeof(CustomDataGrid), null);

        public CustomDataGrid()
        {
            IsEmptyDataGrid = true;
        }

        public bool IsEmptyDataGrid
        {
            get
            {
                return (bool)base.GetValue(IsEmptyDataGridProperty);
            }
            set
            {
                base.SetValue(IsEmptyDataGridProperty, value);
                if(_txtEmptyDataGrid != null)
                {
                    _txtEmptyDataGrid.Visibility = IsEmptyDataGrid ? Visibility.Visible : Visibility.Collapsed;
                }                
            }
        }

        public string EmptyDataGridText
        {
            get
            {
                if (_txtEmptyDataGrid != null)
                {
                    return _txtEmptyDataGrid.Text;
                }

                return (string)base.GetValue(EmptyDataGridTextProperty);
            }
            set
            {
                if (_txtEmptyDataGrid != null)
                {
                    _txtEmptyDataGrid.Text = value;
                }
                base.SetValue(EmptyDataGridTextProperty, value);                
            }
        }

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            _txtEmptyDataGrid = GetTemplateChild("EmptyDataGridTextBlock") as TextBlock;
            if (_txtEmptyDataGrid != null)
            {
                _txtEmptyDataGrid.Text = EmptyDataGridText;
                _txtEmptyDataGrid.Visibility = IsEmptyDataGrid ? Visibility.Visible : Visibility.Collapsed;
            }
        }
    }
}

My XAML is as follows: my:CustomDataGrid Height="180" Name="dgChildren" SelectedItem="{Binding SelectedChild, Mode=TwoWay}" ItemsSource="{Binding Childrens}" IsEmptyDataGrid="{Binding IsEmptyDataGrid}">


In my ViewModel I have the property IsEmptyDataGrid:

public bool IsEmptyDataGrid
{
    get
    {
        return _isEmptyDataGrid;
    }
    set
    {
        _isEmptyDataGrid = value;
        RaisePropertyChanged("IsEmptyDataGrid");
    }
}

my problem is that even though the RaisePropertyChanged("IsEmptyDataGrid") in my view model is hit, it does not go inside the IsEmptyDataGrid property in the custom datagrid and the empty message is displayed along with the data in the source.

What am I doing wrong?

Thanks you in advance, Kruvi

1

1 Answers

1
votes

The setter of a DependencyProperty is not always called in this scenario. Use a Callback function instead:

public static readonly DependencyProperty IsEmptyDataGridProperty = DependencyProperty.Register("IsEmptyDataGrid", typeof(bool), typeof(CustomDataGrid), new UIPropertyMetaData(false,Callback));

private static void Callback(DependecyObject d, DependencyPropertyChangedEventArgs e)
{
    CustomDatagrid cdt = d as CustomDatagrid;
    if(cdt._txtEmptyDataGrid != null)
        {
            cdt._txtEmptyDataGrid.Visibility = cdt.IsEmptyDataGrid ? Visibility.Visible : Visibility.Collapsed;
        }  
}