Hi,
I need some help to get group and ungroup working.
using System;
using System.Collections.ObjectModel;
using System.Windows;
using Northwoods.GoXam.Model;
namespace GoWpfGrouping
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
CreateData();
}
private void CreateData()
{
// model is a GraphModel using GraphModelNodeData<String> as the node data,
// and the node keys are strings
var model = new GraphModel<GraphModelNodeData<String>, String>();
model.NodeKeyPath = "Key"; // use the GraphModelNodeData.Key property
model.ToNodesPath = "ToKeys"; // this node property gets a list of node keys
model.NodeIsGroupPath = "IsSubGraph"; // node property is true if it’s a group
model.GroupNodePath = "SubGraphKey"; // node property gets container’s name
model.NodesSource = new ObservableCollection<GraphModelNodeData<String>>()
{
new GraphModelNodeData<String>()
{
Key = "Alpha",
ToKeys = new ObservableCollection<String>() {"Beta", "Gamma"}
},
new GraphModelNodeData<String>()
{
Key = "Beta",
ToKeys = new ObservableCollection<String>() {"Beta"}
},
new GraphModelNodeData<String>()
{
Key = "Gamma",
ToKeys = new ObservableCollection<String>() {"Delta"},
SubGraphKey = "Epsilon"
},
new GraphModelNodeData<String>()
{
Key = "Delta",
ToKeys = new ObservableCollection<String>() {"Alpha"},
SubGraphKey = "Epsilon"
},
new GraphModelNodeData<String>()
{
Key = "Epsilon",
IsSubGraph = true
},
};
MyDiagram.Model = model;
}
}
}
Just set CommandHandler.PrototypeGroup to a GraphModelNodeData that should be represented by a Group, and the CommandHandler.Group command will be enabled.
If you set CommandHandler.PrototypeGroup, either in XAML as I showed you or in code as you were trying, the Group command should automatically be enabled and it should work.
I suppose if you had set Node.Groupable to false or Diagram.AllowGroup to false, the command would be disabled anyway, but it seems unlikely that you have done this. Or if the model is not modifiable or if the diagram is readonly.
Thank you Walter,
it’s working now. The Problem was only the Binding of the Group- and UngroupCommand in the MenuItem, since MenuItem in ContextMenus are not in the Visual Tree and so somewhat tricky to bind. With the help of StackOverflow I solved this, and now it’s working like a charm.
If I want that the first click (on the group or a node in the group) will always select the group and then a seconde click (on the node this time) will select the node - how can I handle that?
Override ClcikSelectionTool?
I tried this:
public class CustomSelectionTool : ClickSelectingTool
{
public override void DoMouseUp()
{
// group selected before mouseup
var selectedGroup = Diagram.SelectedGroup;
base.DoMouseUp();
// check after mouseup
var selectedPart = Diagram.SelectedPart;
if (selectedPart != null)
{
if (selectedGroup == null || !Equals(selectedGroup, selectedPart.ContainingSubGraph))
{
if (selectedPart.ContainingSubGraph != null)
{
selectedPart.IsSelected = false;
selectedPart.ContainingSubGraph.IsSelected = true;
}
}
}
}
}
I do not recall ever implementing an example with the exact policy that you describe, but later I can search for it. If I find anything useful I’ll post it.
The type of the GraphLinksModel.NodeKeyPath property is a string because that property allows you to customize which property is used to hold the unique “key” for a node data object.
So are you really asking what types can be used as the NodeKey type parameter of GraphLinksModel<NodeType, NodeKey, PortKey, LinkType> ? Basically the NodeKey is used as the key type of several Dictionarys. If you are not going to use a value type as the NodeKey, you’ll need to make it act like one for the purposes of the model’s treating such objects as key values.
Did you mean to bind IsHitTestVisible on the GroupPanel rather than on the outermost StackPanel?
Also, you asked about hit testing, not about selectability. After all, Control-A selects all Selectable Parts without any regard to mouse/finger position. You can bind go:Part.Selectable if you like.
Hi Walter,
I found out that modifing the ClickSelectingTool is not all! If I start to drag a node that is in a group I want also to move the group instead of the node. If the node is selected than the user should be able to move it, but if it is not selected than the drag should not select the node but the group and drag the group instead.
This isn’t really about “Group and ungroup” any more, is it? Could you start a new topic, please? (It isn’t obvious to me as an Administrator that there’s a way for me to change your reply into a new topic, and if you do it it will seem more natural anyway.)