以下方法只能实现一次绑定,如果MyVisible属性在ViewModel中被修改的话,不会影响DataGrid的Column的显示或者隐藏
后台代码
public partial class MainWindow : Window, INotifyPropertyChanged { public MainWindow() { InitializeComponent(); this.DataContext = this; } private IList mockData; public IList MockData { get { if (mockData == null) { mockData = new List (); mockData.Add(new DataGridItem("name1", "description_001", "message_001")); mockData.Add(new DataGridItem("name_002", "description_002", "message_002")); mockData.Add(new DataGridItem("name3", "description_003", "message_003")); mockData.Add(new DataGridItem("name_004", "description_004", "message_004")); mockData.Add(new DataGridItem("name5", "description_005", "message_005")); mockData.Add(new DataGridItem("name_006", "description_006", "message_006")); mockData.Add(new DataGridItem("name7", "description_007", "message_007")); mockData.Add(new DataGridItem("name_008", "description_008", "message_008")); mockData.Add(new DataGridItem("name9", "description_009", "message_009")); } return mockData; } } private Visibility myVisible; public Visibility MyVisible { get { return Visibility.Visible; } set { myVisible = value; } } } public class DataGridItem { public DataGridItem(string name, string description, string message) { this.Name = name; this.Description = description; this.Message = message; } public string Name { get; set; } public string Description { get; set; } public string Message { get; set; } }