Open paulrozhkin opened 2 years ago
When using MVVM, this binding error becomes more evident. Check also Stackoverflow thread
I found an old blogpost that describes a similar problem. The blogpost recommends to globally suppress all binding errors in the app. I did not like this because I only wanted to suppress binding errors that are managed internally by WPF.
Therefore, I ended up writing this workaround for my MVVM app:
Workaround in ViewModel class:
public void RemoveTabItem()
{
AppUtils.BindingErrors.Hide();
// Remove TabItem here
AppUtils.BindingErrors.Show();
}
Workaround:
namespace AppUtils;
using System.Windows;
using System.Diagnostics;
using System.Windows.Threading;
public static class BindingErrors
{
#if !DEBUG
public static void Hide() { }
public static void Show() { }
#endif
#if DEBUG
private static SourceLevels _defaultLevel = PresentationTraceSources.DataBindingSource.Switch.Level;
private static bool _isHiding = false;
public static void Hide()
{
if (!_isHiding)
{
_isHiding = true;
_defaultLevel = PresentationTraceSources.DataBindingSource.Switch.Level;
PresentationTraceSources.DataBindingSource.Switch.Level = SourceLevels.Critical;
}
}
public static void Show()
{
if (_isHiding)
{
/// Wait for UI to load before showing binding errors again
Application.Current.Dispatcher.BeginInvoke(() =>
{
_isHiding = false;
PresentationTraceSources.DataBindingSource.Switch.Level = _defaultLevel;
}, DispatcherPriority.Loaded);
}
}
#endif
}
Does the bug reproduce also in WPF for .NET Framework 4.8?: Yes
Problem description: Binding failure occurs when removing last item (last tab) from TabControl. This is independent of whether MVVM or code behind is being used.
Actual behavior: When deleting the last tab I get an error:
Expected behavior: Binding exception does not occur
Minimal repro: Delete tabs with buttons "Remove last" or "Remove all" MainWindow.xaml:
MainWindow.xaml.cs:
namespace TabControlBug { public partial class MainWindow { public MainWindow() { InitializeComponent(); AddItem(); AddItem(); AddItem(); }
}
using System.ComponentModel; using System.Runtime.CompilerServices; using TabControlBug.Annotations;
namespace TabControlBug { public class TabItemViewModel : INotifyPropertyChanged { private string _text; private string _header;
}