Search code examples
c#wpfwpfdatagrid

Datagrid Cell content is not visible in WPF


I have a datagrid with 8 columns,concatenating the first 2 column values and bind it to the 7th column for every rows.[binding process is done in calendar closed event of the 6th column]In this after binding the 7th column value the bonded value is not visible in the 7 cell of the particular row but if i double clicked on the particular cell then the data is visible...I don't know what is going wrong...

This is the image of my Datagrid

public class pojo
{
    public string Prefix { get; set; }
    public int Year { get; set; }
    public int QuarterNo { get; set; }
    public int SerialNo { get; set; }
    public string From { get; set; }
    public string To { get; set; }
    public string PeriodName { get; set; }
}

ObservableCollection<pojo> models = new ObservableCollection<pojo>();

private void Period_Name(object sender, RoutedEventArgs e)
{
    var rowdata = calendarmstrDG.SelectedItem as pojo;
    var prefix = rowdata.Prefix;
    int year = rowdata.Year;
    int year_twodigits = year % 100;
    string sth = prefix + '-' + year_twodigits.ToString();
    pojo obj = (pojo)calendarmstrDG.SelectedItem;
    obj.PeriodName = sth;
}

Xaml code for 8th column

 <DataGridTextColumn Binding="{Binding Path=PeriodName}"  Width="85" IsReadOnly="True" Header="Period Name"/>

Solution

  • Class should look like this (other fields are omitted):

    public class pojo : INotifyPropertyChanged
    {
    
        private string _prefix;
        private int _year;
    
        public string Prefix
        {
            get => _prefix;
            set
            {
                if (_prefix == value) return;
                _prefix = value;
                OnPropertyChanged(nameof(PeriodName));
            }
        }
        public int Year
        {
            get => _year;
            set
            {
                if (_year == value) return;
                _year = value;
                OnPropertyChanged(nameof(PeriodName));
            }
        }
        public string PeriodName => _prefix + '-' + (_year % 100).ToString(); 
    
        public event PropertyChangedEventHandler PropertyChanged;
        void OnPropertyChanged(string prop)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(prop));
        }
    }