简体   繁体   English

为什么绑定值更改时未引用属性

[英]Why property is not referenced when binded value changes

I realized that Property get/set is not referenced when value changes in DataGridView (DataTable is binded to DataGridView) 我意识到当DataGridView中的值更改(DataTable绑定到DataGridView)时,没有引用属性get / set。

My Code is like this 我的代码是这样的

    private DataTable _dt;
    public DataTable Dt
    {
        get
        {
            return _dt;
        }
        set
        {
            _dt = value;
        }
    }

    public Form1()
    {
        InitializeComponent();

        Dt = new DataTable();
        Dt.Columns.Add("Age");
        Dt.Columns.Add("Name");
        Dt.Rows.Add(19, "kim");
        Dt.Rows.Add(25, "Lee");

        dataGridView1.DataSource = Dt;
    }

As i unsderstand, binding means that if UI control value changes, original value also changes. 如我所知,绑定意味着如果UI控件值更改,则原始值也会更改。 So I expected that DataTable's Set Property is referenced when i change value on DataGridView. 因此,我希望在更改DataGridView的值时引用DataTable的Set属性。 but didn't. 但是没有。

Please someone explain why is so. 请有人解释为什么会这样。

What you describe is true of MVVM when you are using WPF usually, but is common in WinForms too. 通常,当您使用WPF时,您所描述的内容对MVVM来说是正确的,但在WinForms中也很常见。 You are setting the data source for a whole datagrid though. 但是,您正在为整个数据网格设置数据源。 When you change a value inside the grid this will not trigger set Dt to be called. 当您在网格内更改值时,这不会触发集Dt被调用。 If you want to be notified when a cell value changes you would need to use one of the events of the grid. 如果要在单元格值更改时收到通知,则需要使用网格事件之一。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 绑定对象的属性更改时,WPF Caliburn Micro CanExecute - WPF Caliburn Micro CanExecute when property of binded object changes 当属性在内部更改时,如何将控件绑定到属性更改? - How can i have controls binded to a property change when the property changes internally? WPF属性值不同于绑定值 - WPF Property value is different from binded value 为什么我需要一个评估员而不是直接从我的绑定对象数据属性获取我的值 - Why do I need an assessor instead of getting directly my value from my binded object data property 为什么绑定的TextBlock的值在绑定的属性发生更改并调用PropertyChanged事件后不能更改? - Why binded TextBlock`s value can be not changed after the property to which it is bounded changed and event PropertyChanged invoked? 当我尝试访问绑定的 object 的属性时,为什么 Xamarin 不呈现任何内容? - Why Xamarin renders nothing when I try to access a property of the binded object? 为什么更改绑定的页面属性时wpf中的主窗口没有更新? - Why is my main window in wpf not updated when I change a binded page property? 为什么在 ObservableCollection 中引用该属性时,此绑定不起作用? - Why does this Binding not work, when the property is referenced in a ObservableCollection? 属性或变量更改值时触发事件 - Fire event when a property or variable changes value 数值更改时,NumericUpDown DataBinding属性不会更新 - NumericUpDown DataBinding property not updating when value changes
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM