【问题标题】:Ui Not updating when property updated in codebehind在代码隐藏中更新属性时UI不更新
【发布时间】:2017-10-14 10:47:38
【问题描述】:

我想在更改代码后面的属性时更新我的​​数据网格单元格。我的代码是 模型(实体)

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;

在这里我想更改代码中的 ID,我的问题是网格单元没有更新。请帮我解决我的问题。我可以使用 ObservableCollection 来实现这一点。 谢谢

【问题讨论】:

    标签: wpf mvvm binding


    【解决方案1】:

    给定的代码不完整,我无法弄清楚问题所在。 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 作为数据网格绑定 (ItemsSource="{Binding IGdaily}" )
    • ItemsSource 应该是一个集合。
    猜你喜欢
    • 1970-01-01
    • 2011-07-09
    • 2020-02-11
    • 2018-06-28
    • 1970-01-01
    • 1970-01-01
    • 2022-11-12
    • 1970-01-01
    相关资源
    最近更新 更多