MSAGL: WpfGraphControl: Как получить объект на графике после щелчка?C#

Место общения программистов C#
Anonymous
MSAGL: WpfGraphControl: Как получить объект на графике после щелчка?

Сообщение Anonymous »

Я создаю приложение в WPF с использованием MVVM, и мне нужно получить объект (узел, ребро) после щелчка мышью по графику, чтобы обработать его с помощью разными способами.
Но когда я нажимаю на график, я получаю объект sender, равный нулю после приведения его к типу GraphViewer. Объект sender извлекается по типу Microsoft.Msagl.WpfGraphControl.AutomaticGraphLayoutControl, и я не знаю, как получить этот объект, поскольку этот тип не имеет такой возможности. Да, этот тип (Microsoft.Msagl.WpfGraphControl.AutomaticGraphLayoutControl) имеет свойство _graphViewer с типом GraphViewer, но оно частное .
Итак, вот мой код.
Просмотр xaml:







Просмотр кода:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataContext = new ApplicationViewModel(new DefaultDialogService(), new MSAGLFileService());
}
private void graphDisplay_MouseDown(object sender, MouseButtonEventArgs e)
{
var graphViewer = sender as GraphViewer;
if (graphViewer != null)
{
if(graphViewer.ObjectUnderMouseCursor as IViewerNode != null)
MessageBox.Show("You click on node");
else if(graphViewer.ObjectUnderMouseCursor as IViewerEdge != null)
MessageBox.Show("You click on edge");
else
MessageBox.Show("You click not on edge or not on node");
}
else
MessageBox.Show("sender is null");
}
}

ApplicationViewModel:
public class ApplicationViewModel
{
private GraphModel currentGraph;
public GraphModel CurrentGraph
{
get { return currentGraph; }
set { currentGraph = value; OnPropertyChanged(nameof(CurrentGraph)); }
}
public ApplicationViewModel(IDialogService dialogService, IFileService fileService)
{
currentGraph = new GraphModel();
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = "")
{
if (PropertyChanged != null)
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}

Графическая модель:
private string name { get; set; }
private Graph graphObject { get; set; }
private bool isInitialized { get; set; }
public string Name
{
get{ return name; }
set { name = value; OnPropertyChanged(nameof(Name)); }
}
public Graph GraphObject
{
get{ return graphObject; }
set{ graphObject = value; OnPropertyChanged(nameof(GraphObject));}
}
public bool IsInitialized
{
get{ return isInitialized; }

set{i sInitialized = value; OnPropertyChanged(nameof(IsInitialized));}
}
public GraphModel()
{
IsInitialized = false;
Name = " - ";
GraphObject = new Graph(Name);
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = "")
{
if (PropertyChanged != null)
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}


Подробнее здесь: https://stackoverflow.com/questions/789 ... r-clicking

Вернуться в «C#»