比如页面加载的时候执行一个绑定
//楼层
//DataGrid数据模型
List<FloorRoomModel> floorRoomList = new List<FloorRoomModel>();
public class FloorRoomModel
{
public string ID { get; set; }
public FloorRoomModel(string _ID)
{
ID = _ID;
}
public FloorRoomModel() { }
}
//绑定楼层
private void BindFloor(string ID)
{
XElement elFloor = GetElByID("Floor", ID);
//绑定DataGrid
floorRoomList.Clear();
floorRoomList.Add(new FloorRoomModel("1"));
floorRoomList.Add(new FloorRoomModel("2"));
dgFloor.ItemsSource = floorRoomList;
dgFloor.CanUserSortColumns = false;
dgFloor.IsReadOnly = true;
}
点击按钮事件,重新绑定。
//删除
private void btnDelete_Click(object sender, RoutedEventArgs e)
{
//绑定DataGrid
floorRoomList.Clear();
floorRoomList.Add(new FloorRoomModel("2"));
floorRoomList.Add(new FloorRoomModel("3"));
dgFloor.ItemsSource = floorRoomList;
dgFloor.CanUserSortColumns = false;
dgFloor.IsReadOnly = true;
MessageBox.Show("");
}
为什么DataGrid中数据不变化呢?
解决方法就是在绑定之前先置空.
dgFloor.ItemsSource =
null;
dgFloor.ItemsSource =
floorRoomList;
http://www.0379zd.com/news/show/440001.htm
11111111111111