[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;
}
}
}
<Grid>
<TextBlock
Text="{Binding Name}"></TextBlock>
</Grid>
发现success并没有显示:
后面发现绑定的类属性必须带上set和get,否则不会展示结果,修改之后如下:
public class Person
{
public string Name { get; set; }
}
效果:
https://github.com/li-zheng-hao