当代码隐藏更新属性时,不更新
问题描述:
当我更改代码后面的属性时,我想更新我的数据网格单元格。我的代码是 模型(实体)当代码隐藏更新属性时,不更新
public partial class IGdaily
{
public int GDaily_Id { get; set; }
public int Item_Id { get; set; }
}
视图模型
public class Vm_Purchase : INotifyPropertyChanged
{
IGoldEntities db = new IGoldEntities();
//public ObservableCollection<IGdaily> Vm_IGdaily { get; set; }
public IGdaily Obj_IGdaily { get; set; }
public Vm_Purchase()
{
Obj_IGdaily = new IGdaily();
}
public Int32 Item_Id
{
get { return Obj_IGdaily.Item_Id; }
set
{
Obj_IGdaily.Item_Id = value;
NotifyPropertyChanged("Item_Id");
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged([CallerMemberName] string PropertyName = "")
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(PropertyName));
}
}
}
在XAML
<iG:DataGridTextcolumn Binding="{Binding Item_Id, Mode=OneWay}" Header="Item Id" Width="SizeToHeader" />
在主窗口
Vm_Purchase ItmId = new Vm_Purchase();
ItmId.Item_Id = Id;
在这里,我要改变标识的代码,我的概率lem是网格单元格没有更新。请帮我解决我的问题。我可以使用ObservableCollection来达到这个目的吗? 谢谢
答
给定的代码是不完整的,我找不出什么问题。 DataGrid
控件的ItemsSource是什么?它如何与DataGrid
控件绑定?
下面的代码可能是你在找什么:
public partial class IGdaily : INotifyPropertyChanged
{
private int _gDailyId;
private int _itemId;
public int GDaily_Id
{
get { return _gDailyId; }
set
{
_gDailyId = value;
NotifyPropertyChanged();
}
}
public int Item_Id
{
get { return _itemId; }
set
{
_itemId = value;
NotifyPropertyChanged();
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged([CallerMemberName] string PropertyName = "")
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(PropertyName));
}
}
}
在主窗口
public partial class MainWindow : Window
{
public ObservableCollection<IGdaily> Vm_IGdaily { get; } = new ObservableCollection<IGdaily>();
private IGdaily testData = new IGdaily();
public MainWindow()
{
InitializeComponent();
TheDataGridControl.ItemsSource = Vm_IGdaily;
Vm_IGdaily.Add(testData);
}
private void Button1_OnClick(object sender, RoutedEventArgs e)
{
testData.GDaily_Id = 100;
}
}
我用IGdaily作为DataGrid的结合(的ItemsSource = “{结合IGdaily}”) –
'的ItemsSource '应该是一个集合。 – Iron