Category / Section
How to bind the SelectedItems property of SfDiagram to ViewModel property?
1 min read
WPF Diagram (SfDiagram) supports to bindbinding the SelectedItems property of the SfDiagram View class to any custom ViewModel class’s property. You can achieve this requirement by creating a custom ViewModel class with the custom SelectedItems property of SelectorViewModel type. Then, you can bind the SelectedItems property of SfDiagram (View) to view model of SelectedItems property.
XAML
<!--Initialize the SfDiagram and binding custom view model SelectedItems property to SfDiagram's SelectedItems--> <syncfusion:SfDiagram SelectedItems="{Binding SelectedItems}"> </syncfusion:SfDiagram>
C#
//To Represent the view model class for SfDiagram to bind SelectedItems property of SfDiagram view to ViewModel. public class CustomVM : INotifyPropertyChanged { public CustomVM() { } private SelectorViewModel selectedItems = new SelectorViewModel() { Nodes = new ObservableCollection<NodeViewModel>(), Connectors = new ObservableCollection<ConnectorViewModel>(), }; /// <summary> /// Gets or sets the SelectedItems which is to bind the custom SelectorViewModel to SelectedItems property of SfDiagram view. /// </summary> public SelectorViewModel SelectedItems { get { return selectedItems; } set { if (selectedItems != value) { selectedItems = value; OnPropertyChanged("SelectedItems"); } } } }