dotnet / dotnet/winforms

`Control.Visible` binding does not activate when `Visible` was explicitly set to `false` before `DataBindings.Add` was called

Open
#15,067 4 comments 0 reactions 1 assignee Claimed by @SimonZhao888 View on GitHub
area-System.Drawing untriaged
Dominant language
C#
Stars
4.9k
Forks
1.1k
Avg merge
20h 23m
Merged PRs (30d)
103

Description

### Description

I manage to reproduce the problem using a very short sample

Here's the code:

## Code

```csharp
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

var viewModel = new ViewModel();

var form = new Form { Text = "Panel.Visible binding repro", Width = 400, Height = 260 };

var checkBox = new CheckBox
{
Text = "Enable",
Checked = viewModel.EnableToggle,
Location = new Point(20, 20),
AutoSize = true
};

// Panel A: Visible explicitly set to false BEFORE binding.
var panelA = new Panel
{
Location = new Point(20, 60),
Size = new Size(300, 50),
BorderStyle = BorderStyle.FixedSingle,
Visible = false
};
panelA.Controls.Add(new Label { Text = "Panel A", AutoSize = true, Location = new Point(10, 10) });
panelA.DataBindings.Add(nameof(Panel.Visible), viewModel, nameof(ViewModel.EnableToggle));

// Panel B: left at its default Visible (true), never assigned before binding.
var panelB = new Panel
{
Location = new Point(20, 130),
Size = new Size(300, 50),
BorderStyle = BorderStyle.FixedSingle
};
panelB.Controls.Add(new Label { Text = "Panel B", AutoSize = true, Location = new Point(10, 10) });
panelB.DataBindings.Add(nameof(Panel.Visible), viewModel, nameof(ViewModel.EnableToggle));

checkBox.CheckedChanged += (_, _) => viewModel.EnableToggle = checkBox.Checked;

form.Controls.Add(checkBox);
form.Controls.Add(panelA);
form.Controls.Add(panelB);

Application.Run(form);

class ViewModel : INotifyPropertyChanged
{
private bool _enableToggle = true;
public event PropertyChangedEventHandler? PropertyChanged;

public bool EnableToggle
{
get => _enableToggle;
set
{
if (_enableToggle == value) return;
_enableToggle = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(EnableToggle)));
}
}
}
```

With the csproject:

```
`Exe`, `true`, and
`net6.0-windows` or later (tested on `net10.0-windows`)
```

## Repro steps

1. Build and run the attached project (or the minimal snippet below).
2. Toggle the "Enable" checkbox a few times.
3. Watch Panel A (labelled "Panel A content") and Panel B (labelled "Panel B content").
4. Panel A ( with `Visible=false` initially) will not toggle, unlike Panel B ( which is with the default).

## Environment

Tested on .net 8.0 and .net 10.0

### Reproduction Steps

See above

### Expected behavior

Both panels should be able to show or unshow

### Actual behavior

Only Panel B is able to show and unshow, Panel A is not.

### Regression?

_No response_

### Known Workarounds

_No response_

### Configuration

_No response_

### Other information

_No response_

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.