miroiu / nodify

Highly performant and modular controls for node-based editors designed for data-binding and MVVM.
https://miroiu.github.io/nodify
MIT License
1.3k stars 208 forks source link

[Bug] Cannot bind a Context Menu to Input/Output Connector #61

Closed AlexDialga closed 1 year ago

AlexDialga commented 1 year ago

Describe the bug I created a ConnectorViewModel with ContextMenu, and try to bind to ContextMenu of nodify:NodeInput in EditorView, but it can't be open.

To Reproduce

EditorView.xaml

<!--...-->
<nodify:Node.InputConnectorTemplate>
    <DataTemplate DataType="{x:Type localConnectors:IConnectorViewModel`1}">
        <nodify:NodeInput
            ...
            ContextMenu="{Binding ConnectorContextMenu}"
            ...
            >
<!--...-->

ConnectorViewModel.cs

using System.Windows.Controls
using CommunityToolkit.Mvvm.ComponentModel;

public interface IConnectorViewModel : IObservableObject
{
  //...
  ContextMenu? ConnectorContextMenu { get; set; }
  //...
}

public class ConnectorViewModel : ObservableRecipient, IConnectorViewModel 
{
  //...
  protected ContextMenu? connectorContextMenu = new();
  public virtual ContextMenu? ConnectorContextMenu { get => this.connectorContextMenu; 
      set => SetProperty(ref this.connectorContextMenu, value); }

  protected ObservableCollection<object> connectorContextMenuItems
      = new ObservableCollection<object>();
  public virtual ObservableCollection<object> ConnectorContextMenuItems {
      get => this.connectorContextMenuItems;
      set => SetProperty(ref this.connectorContextMenuItems, value); }
  //...
  public ConnectorViewModel()
  {
    var menuItem = new MenuItem() { 
        Header = "TheMenuItem",
        Name = "TheMenuItem" };
    this.ConnectorContextMenuItems.Add(menuItem);
    this.ConnectorContextMenu = new() { ItemsSource = this.ConnectorContextMenuItems };
  }
  //...
}

public interface IConnectorViewModel<T> : IConnectorViewModel
{
  //...
}

public class ConnectorViewModel<T> : ConnectorViewModel
  , IConnectorViewModel<T>
{
  //...
}

Expected behavior You can create a NodeViewModel and add a ContextMenu and a ContextMenuItems, and bind to the nodify:Node in EditorView. NodeContextMenuTriggered

miroiu commented 1 year ago

Hi, thanks for reporting the bug! I published the fix. Can you please update to the latest version and try again?

AlexDialga commented 1 year ago

image image image

Amazing! Now connectors can add different context menu now!