2016-11-12 1 views
0

Syncfusion WPF 컨트롤을 사용하고 있습니다. 방사형 트리 다이어그램을 표시하는 프로젝트를 만들었습니다.선택한 노드의 기본 콘텐츠 얻기

그림의 맨 아래에있는 Content (Link, LinkID, ParentID 등)에 나열된 기본 값에 액세스하려고합니다.

사진 : 나는 node.Content.GetType().ToString()을 실행하면

enter image description here

에서, NodeSelected 이벤트를 보면, 난 액세스하려는 속성이 포함되어 내 기본 클래스 모델을 얻을.

그래서 노드를 선택하는 방법, 노드의 데이터 인 기본 클래스의 속성을 가져 오는 방법을 잘 모르겠습니다.

모든 것이 의미가 있기를 바랍니다. 나는 스스로 가르칩니다.

가 여기 내 MainWindow를 코드입니다 :

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Windows; 
using Syncfusion; 
using System.Windows.Controls; 
using System.Windows.Data; 
using System.Windows.Documents; 
using System.Windows.Input; 
using System.Windows.Media; 
using System.Windows.Media.Imaging; 
using System.Windows.Navigation; 
using System.Windows.Shapes; 
using Syncfusion.UI.Xaml.Diagram.Layout; 
using Syncfusion.UI.Xaml.Diagram; 
using System.Collections.ObjectModel; 
using Link_Map.Classes; 
using Syncfusion.SfSkinManager; 
using Syncfusion.Windows.Shared; 
using Syncfusion.Windows.Tools.Controls; 
using Syncfusion.UI.Xaml.Diagram.Controls; 

namespace Link_Map 
{ 
    /// <summary> 
    /// Interaction logic for MainWindow.xaml 
    /// </summary> 
    public partial class MainWindow : RibbonWindow 
    { 
     Controller controller = new Controller(); 
     DirectedTreeLayout tree = new DirectedTreeLayout(); 
     ObservableCollection<Node> nodes = new ObservableCollection<Node>(); 
     ObservableCollection<Connector> connects = new ObservableCollection<Connector>(); 
     public MainWindow() 
     { 
      InitializeComponent(); 
      InitDiagram(); 
      controller.AddRoot(); 
      Link_Grid.ItemsSource = controller.viewmodel.Links; 
      SkinStorage.SetVisualStyle(this, "Blend"); 
      //nodes.ElementAt(0).IsSelected = true; 
     } 

     private void InitDiagram() 
     { 
      controller.Init(); 
      lmWindow.Nodes = nodes; 
      lmWindow.Connectors = connects; 
      DataSourceSettings settings = new DataSourceSettings(); 
      settings.DataSource = controller.viewmodel.Links; 
      settings.ParentId = "ParentID"; 
      settings.Id = "LinkID"; 
      settings.Root = "0"; 
      lmWindow.DataSourceSettings = settings; 
      settings.DataSource = controller.viewmodel.Links; 
      (lmWindow.Info as IGraphInfo).ItemAdded += MainWindow_ItemAdded; 
      (lmWindow.Info as IGraphInfo).ItemSelectedEvent += NodeSelected; 

      lmWindow.LayoutManager = new LayoutManager() 
      { 
       Layout = new RadialTreeLayout() 
      }; 



      (lmWindow.LayoutManager.Layout as RadialTreeLayout).HorizontalSpacing = 10; 
      (lmWindow.LayoutManager.Layout as RadialTreeLayout).VerticalSpacing = 35; 



      //lmWindow.Tool = Tool.SingleSelect; 
      //lmWindow.Tool = Tool.ZoomPan; 
      lmWindow.Menu = null; 
      lmWindow.ScrollSettings.ScrollLimit = ScrollLimit.Diagram; 
      lmWindow.DefaultConnectorType = ConnectorType.Line; 
      lmWindow.PageSettings.PageBorderBrush = new SolidColorBrush(Colors.Transparent); 


      SelectorViewModel svm = (lmWindow.SelectedItems as SelectorViewModel); 
      svm.SelectorConstraints = svm.SelectorConstraints & ~(SelectorConstraints.QuickCommands|SelectorConstraints.Resizer); 




      //lmWindow.Constraints = SelectorConstraints.QuickCommands; 

      //lmWindow.Constraints = lmWindow.Constraints & ~(GraphConstraints.Draggable | GraphConstraints.Selectable); 


    } 

     public void NodeSelected(object sender, DiagramEventArgs e) 
     { 
      Node node = (e.Item as Node); 
      string ders = node.Content.GetType().ToString(); 
      Console.WriteLine(ders); 
     } 

     private void Metro_Theme_Click(object sender, RoutedEventArgs e) 
     { 
      SkinStorage.SetVisualStyle(this, "Metro"); 
     } 

     private void Blend_Theme_Click(object sender, RoutedEventArgs e) 
     { 
      SkinStorage.SetVisualStyle(this, "Blend"); 
     } 

     private void Add_Click(object sender, RoutedEventArgs e) 
     { 
      for(int i=0; i<10; i++) 
      { 
       controller.AddLink(); 
      } 
      lmWindow.LayoutManager.Layout.UpdateLayout(); 
     } 

     //https://www.syncfusion.com/forums/127392/how-to-fire-event-on-sfdiagram-node-selection 
     //Apply Node and Connector Style 
     private void MainWindow_ItemAdded(object sender, ItemAddedEventArgs args) 
     { 
      if (args.Item is INode) 
      { 
       (args.Item as INode).UnitWidth = 10; 
       (args.Item as INode).UnitHeight = 10; 
       (args.Item as INode).ContentTemplate = this.lmWindow.Resources["contentTemplate"] as DataTemplate; 
      } 
      else if (args.Item is IConnector) 
      { 
       SolidColorBrush solid = new SolidColorBrush(); 
       solid.Color = Color.FromArgb(255, 186, 186, 186); 
       (args.Item as IConnector).TargetDecoratorStyle = this.Resources["style"] as Style; 
      } 
     } 

     private void Link_Grid_GridPasteContent(object sender, Syncfusion.UI.Xaml.Grid.GridCopyPasteEventArgs e) 
     { 

     } 
    } 
} 

답변

0

글쎄, 난 내 질문을 해결하지만, 작동 왜 아무 생각이 없습니다.

나는 스스로 가르치기 때문에 나는 그것이 작동 할 때까지 생각할 수있는 모든 것을 시도해 본다. (node)).Content)).link 내가 원하는 속성 이었지만

public void NodeSelected(object sender, DiagramEventArgs e) 
{ 
    Node node = (e.Item as Node); 
    string ders = ((Link_Map.Classes.Links)(((System.Windows.Controls.ContentControl)(node)).Content)).link; 
    Console.WriteLine(ders); 
} 

, 나는 지금 여기에서 기본 클래스의 속성에 액세스 할 수 있습니다 내가 원하고 기본 클래스의 컨텐츠를 얻을 수있었습니다.

누군가 더 좋은 방법이 있다면 게시 해주세요!

0

요구 사항 : "선택한 노드의 컨텐츠 속성을 가져와야합니다."

웨이 1 : 캐스팅 : 캐스트를 수행하려면 변환 할 값 또는 변수 앞에 괄호 안에 캐스팅 할 유형을 지정하십시오. 이를 나타 내기위한 코드 예제를 제공했습니다. 아래 코드 예제를 참조하십시오.

방법 2 : foreach_Reflection을 통해 속성 반복 : GetProperties 메서드 (FrameWork)는 클래스 모델 속성을 가져 오는 데 사용되며 속성 값을 찾습니다. 아래 코드 예제를 참조하십시오.

코드 예 :

여기
//ItemSelectedEvent 
(diagram.Info as IGraphInfo).ItemSelectedEvent += MainWindow_ItemSelectedEvent; 

void MainWindow_ItemSelectedEvent(object sender, DiagramEventArgs args) 
{ 
    INode node = args.Item as INode; 
    //Way 1:casting 
    string des = (node.Content as Employee).EmpId.ToString(); 
    Console.WriteLine(des); 

    //Way 2: Iterate the Propeties via foreach 
    foreach(var prop in node.Content.GetType().GetProperties()) 
    { 
     string des= prop.GetValue(node.Content).ToString(); 
     Console.WriteLine(des); 
    } 
} 

, 다이어그램은 SfDiagram의 인스턴스이며, 직원은 기본 클래스입니다.

+0

감사합니다.이 솔루션은 내 것보다 깨끗합니다. 이런 것들은 Syncfusion 문서에 있어야합니다. – Cnote