DataGrid单元格内容在WPF中不可见

时间:2017-12-23 07:41:11

标签: c# wpf wpfdatagrid

我有一个包含8列的数据网格,连接前2列值并将其绑定到每行的第7列。[绑定过程在第6列的日历关闭事件中完成]在此之后绑定第7列值在特定行的7个单元格中看不到绑定值,但如果我双击特定单元格,则数据可见...我不知道出了什么问题......

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;
}

第8列的Xaml代码

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

1 个答案:

答案 0 :(得分:0)

类应如下所示(省略其他字段):

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));
    }
}