[WPF]DataContext结果不显示

namespace DataContext_ItemSource_Demo
{
    public class Person
    {
        public string Name;
    }
    public class ViewModel
    {
        public MainWindow win;
        public Person person;
        public ViewModel(MainWindow window)
        {
            this.win = window;
            var per = new Person()
            {
                Name = "success"
            };
            win.DataContext = per;
        }
    }
}
 
        
    

 

发现success并没有显示:

[WPF]DataContext结果不显示_第1张图片

 

后面发现绑定的类属性必须带上set和get,否则不会展示结果,修改之后如下:

 

 public class Person
 {
     public string Name { get; set; }
 }

效果:

[WPF]DataContext结果不显示_第2张图片

转载于:https://www.cnblogs.com/lizhenghao126/p/11053600.html

你可能感兴趣的:([WPF]DataContext结果不显示)