Я пытаюсь реализовать сценарий master / detail с шаблоном MVVM в проекте Windows Forms (я злюсь, я знаю). Рассмотрим следующие виды моделей:
public class MasterViewModel
{
public BindingList<DetailViewModel> Details { get; set; }
public DetailViewModel SelectedDetail
{
get
{
//
}
set
{
// raises SelectedDetailChanged
}
}
}
public class DetailViewModel
{
public string SubProperty
{
get
{
// ...
}
set
{
// ... raises SubPropertyChanged
}
}
}
Я пытаюсь привязать SubProperty DetailViewModel к TextBox, используя следующий код (и путь свойства, поддерживаемый привязкой данных Windows Forms):
MasterViewModel masterViewModel;
TextBox textBox;
// ...
Binding binding = new Binding("Text", masterViewModel, "SelectedDetail.SubProperty");
binding.FormattingEnabled = true;
binding.DataSourceUpdateMode = DataSourceUpdateMode.OnPropertyChanged;
binding.ControlUpdateMode = ControlUpdateMode.OnPropertyChanged;
textBox.DataBindings.Add(binding);
Это работает безупречно (как WPF !!!) ... до тех пор, пока SelectedDetail не станет нулевым (по моей логике, SelectedDetail null означает, что в главном представлении ничего не выбрано). Если SelectedDetail равен нулю, я получил ArgumentNullException (имя параметра: компонент).
Есть ли способ обработать обнуляемость свойства "parent" (внутри пути навигации)?
Вот трассировка стека исключений:
in System.ComponentModel.ReflectPropertyDescriptor.AddValueChanged(Object component, EventHandler handler)
in System.Windows.Forms.BindToObject.CheckBinding()
in System.Windows.Forms.BindToObject.SetBindingManagerBase(BindingManagerBase lManager)
in System.Windows.Forms.Binding.SetListManager(BindingManagerBase bindingManagerBase)
in System.Windows.Forms.ListManagerBindingsCollection.AddCore(Binding dataBinding)
in System.Windows.Forms.BindingsCollection.Add(Binding binding)
in System.Windows.Forms.BindingContext.UpdateBinding(BindingContext newBindingContext, Binding binding)
in System.Windows.Forms.Control.UpdateBindings()
in System.Windows.Forms.Control.OnBindingContextChanged(EventArgs e)
in System.Windows.Forms.Control.OnParentBindingContextChanged(EventArgs e)
in System.Windows.Forms.Control.OnBindingContextChanged(EventArgs e)
in System.Windows.Forms.Control.OnParentBindingContextChanged(EventArgs e)
in System.Windows.Forms.Control.OnBindingContextChanged(EventArgs e)
in System.Windows.Forms.Control.OnParentBindingContextChanged(EventArgs e)
in System.Windows.Forms.Control.OnBindingContextChanged(EventArgs e)
in System.Windows.Forms.Control.OnParentBindingContextChanged(EventArgs e)
in System.Windows.Forms.Control.OnBindingContextChanged(EventArgs e)
in System.Windows.Forms.Control.OnParentBindingContextChanged(EventArgs e)
in System.Windows.Forms.Control.OnBindingContextChanged(EventArgs e)
in System.Windows.Forms.Control.set_BindingContextInternal(BindingContext value)
in System.Windows.Forms.ContainerControl.set_BindingContext(BindingContext value)
in System.Windows.Forms.ContainerControl.get_BindingContext()
in System.Windows.Forms.Control.get_BindingContextInternal()
in System.Windows.Forms.ContainerControl.get_BindingContext()
in System.Windows.Forms.Control.get_BindingContextInternal()
in System.Windows.Forms.ContainerControl.get_BindingContext()
in System.Windows.Forms.Control.get_BindingContextInternal()
in System.Windows.Forms.ContainerControl.get_BindingContext()
in System.Windows.Forms.Control.get_BindingContextInternal()
in System.Windows.Forms.Control.get_BindingContext()
in System.Windows.Forms.Control.get_BindingContextInternal()
in System.Windows.Forms.Control.get_BindingContext()
in System.Windows.Forms.Control.UpdateBindings()
in System.Windows.Forms.Control.OnBindingContextChanged(EventArgs e)
in System.Windows.Forms.DataGridView.OnBindingContextChanged(EventArgs e)
in System.Windows.Forms.Control.OnParentBindingContextChanged(EventArgs e)
in System.Windows.Forms.Control.OnBindingContextChanged(EventArgs e)
in System.Windows.Forms.Control.OnParentBindingContextChanged(EventArgs e)
in System.Windows.Forms.Control.OnBindingContextChanged(EventArgs e)
in System.Windows.Forms.ContainerControl.OnCreateControl()
in System.Windows.Forms.UserControl.OnCreateControl()
in System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
in System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
in System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
in System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
in System.Windows.Forms.Control.CreateControl()
in System.Windows.Forms.Control.WmShowWindow(Message& m)
in System.Windows.Forms.Control.WndProc(Message& m)
in System.Windows.Forms.ScrollableControl.WndProc(Message& m)
in System.Windows.Forms.ContainerControl.WndProc(Message& m)
in System.Windows.Forms.Form.WmShowWindow(Message& m)
in System.Windows.Forms.Form.WndProc(Message& m)
...
in System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
in System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
in System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)