You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

为何Control.ResetBindings()会清除控件DataBindings?与MSDN描述不符

Why ResetBindings() Removes Your ComboBox Binding?

Let's break down what's happening here—your observation makes total sense once you look past the specific context in the MSDN description.

First, let's recap your code for clarity:

ComboBox comboBox = new ComboBox();
comboBox.DataBindings.Add(new Binding("SelectedItem", "", null));
Debug.WriteLine($"Bindings count = {comboBox.DataBindings.Count}"); // Outputs 1
comboBox.ResetBindings();
Debug.WriteLine($"Bindings count = {comboBox.DataBindings.Count}"); // Outputs 0

The MSDN Context You're Missing

The MSDN quote you referenced specifically applies to controls bound to a valid BindingSource. That's a critical detail! The ResetBindings() method behaves differently depending on whether your binding is linked to an operational data source or not.

Why Your Binding Gets Removed

Your code creates an invalid, incomplete binding:

  • The second parameter to Binding is an empty string (""), meaning there's no actual data source object to bind to.
  • The third parameter is null, so there's no property path on a source object to link with the ComboBox's SelectedItem.

When you call ResetBindings(), the ComboBox doesn't just "refresh values" blindly—it first validates the bindings. For bindings with no valid data source (like yours), the control cleans them up entirely because there's nothing to refresh from. The method can't re-read items or update display values if there's no source to pull data from!

The Correct Scenario (Matching MSDN)

If you use a valid BindingSource for your binding, ResetBindings() behaves exactly as MSDN describes—no bindings get removed, it just refreshes the displayed values. Here's a corrected example:

// Sample data object
public class ListItem
{
    public string Label { get; set; }
}

// Set up a valid BindingSource with data
var bindingSource = new BindingSource();
bindingSource.Add(new ListItem { Label = "First Item" });
bindingSource.Add(new ListItem { Label = "Second Item" });

ComboBox comboBox = new ComboBox();
// Bind to a valid source (and set display member for visibility)
comboBox.DataBindings.Add(new Binding("SelectedItem", bindingSource, "", true));
comboBox.DisplayMember = "Label";

Debug.WriteLine($"Bindings count = {comboBox.DataBindings.Count}"); // Outputs 1
comboBox.ResetBindings();
Debug.WriteLine($"Bindings count = {comboBox.DataBindings.Count}"); // Still outputs 1

Key Takeaway

ResetBindings() only preserves bindings when there's a valid, operational data source (like a BindingSource) to refresh from. Invalid bindings—those with no source or no property path—get discarded because the method can't fulfill its core purpose of refreshing data for them.

内容的提问来源于stack exchange,提问作者LINQ2Vodka

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.13 08:13:48