Our What's New in v24.2 webpage includes product-specific surveys. Your response to our survey questions will help us measure product satisfaction for features released in this major update and help us refine our plans for our next major release.
Fires when the diagram is about to be saved (when an end-user uses Save actions in the Diagram’s Ribbon menu, or when the DiagramControl.SaveFile/DiagramControl.SaveFileAs method is called). The event allows you to implement custom saving logic.
This example demonstrates how to open and save diagrams to a custom storage (e.g., a database) instead of a file system. In the example, the following events are used to implement this functionality:
DiagramControl.ShowingOpenDialog - This event fires before the standard Open dialog is shown and allows you to customize the dialog options or replace the standard dialog with a custom one. You can also cancel the Open operation by setting the e.Cancel parameter to true.
DiagramControl.ShowingSaveDialog - Similarly to the ShowingOpenDialog event, the ShowingSaveDialog event allows you to customize the standard Save dialog in DiagramControl or replace it with a custom one. Setting the e.Cancel parameter to true will cancel the Save operation.
DiagramControl.CustomLoadDocument - This event fires after a user selected a document in the Open dialog or the DiagramControl.DocumentSource property was set in code. The event exposes the selected document source (e.g., a document name or a file stream) through the e.DocumentSource property and allows you to implement your own loading logic. For example, you can retrieve a diagram file from a database and load it into DiagramControl using the DiagramControl.LoadDocument property (as demonstrated in the example) or populate the diagram with items manually. After implementing your custom loading logic, set the e.Handled parameter to true, so that DiagramControl does not load the previously selected document source.
DiagramControl.CustomSaveDocument - The CustomSaveDocument event allows you to specify custom saving logic for your diagram. The event fires after the Save operation was initiated and selection was made in the Save dialog (if there was a dialog). The e.DocumentSource property specifies the default location (file name, stream, etc.) where the diagram will be saved. You can set the e.Handled parameter to true to cancel the standard saving logic and implement your custom one. For example, save the diagram to a stream using the DiagramControl.SaveDocument method as demonstrated in the example or iterate through diagram items manually and read required information.
ImportsDevExpress.Diagram.CoreImportsDevExpress.Xpf.CoreImportsDevExpress.Xpf.DiagramImportsSystemImportsSystem.Collections.GenericImportsSystem.Data.EntityImportsSystem.IOImportsSystem.LinqImports System.TextImportsSystem.Threading.TasksImportsSystem.WindowsNamespace DXDiagram.CustomDiagramStorage
FriendClass DiagramStorageInitializer
Inherits DropCreateDatabaseIfModelChanges(Of DiagramStorage)
ProtectedOverridesSub Seed(ByVal storage As DiagramStorage)
MyBase.Seed(storage)
Dim diagram = New DiagramControl()
For i AsInteger = 0To4
diagram.Items.Add(New DiagramShape() With {.Position = New Point(200, 100 + i * 100), .Width = 100, .Height = 50, .Content = "Item " & (i + 1).ToString()})
If i = 0ThenContinueForEndIfUsing stream = New MemoryStream()
diagram.SaveDocument(stream)
Dim diagramData = New DiagramData() With {.Name = (i + 1).ToString() & " items", .Data = stream.ToArray()}
storage.DiagramData.Add(diagramData)
EndUsingNext i
storage.SaveChanges()
EndSubEndClassEndNamespace
ImportsSystemImportsSystem.Collections.GenericImportsSystem.LinqImports System.TextImportsSystem.WindowsImportsSystem.Windows.ControlsImportsSystem.Windows.DataImportsSystem.Windows.DocumentsImportsSystem.Windows.InputImportsSystem.Windows.MediaImportsSystem.Windows.Media.ImagingImportsSystem.Windows.NavigationImportsSystem.Windows.ShapesImportsDevExpress.Xpf.CoreImportsDevExpress.Diagram.CoreImportsDevExpress.MvvmImports DevExpress.Mvvm.POCO
ImportsSystem.IONamespace DXDiagram.CustomDiagramStorage
'''<summary>''' Interaction logic for MainWindow.xaml'''</summary>PartialPublicClass MainWindow
Inherits DXWindow
PublicSubNew()
InitializeComponent()
EndSubPrivateSub OnLoaded(ByVal sender AsObject, ByVal e As RoutedEventArgs)
Dispatcher.BeginInvoke(New Action(AddressOf diagram.OpenFile))
EndSubPrivateSub OnShowingOpenDialog(ByVal sender AsObject, ByVal e As DevExpress.Xpf.Diagram.DiagramShowingOpenDialogEventArgs)
Dim viewModel = SelectDiagramViewModel.Create()
Dim result = openDialogService.ShowDialog(MessageButton.OKCancel, "Choose a diagram to open", viewModel)
If result = MessageResult.OK Then
e.DocumentSourceToOpen = viewModel.SelectedName
Else
e.Cancel = TrueEndIfEndSubPrivateSub OnCustomLoadDocument(ByVal sender AsObject, ByVal e As DevExpress.Xpf.Diagram.DiagramCustomLoadDocumentEventArgs)
If e.DocumentSource IsNothingThen
diagram.NewDocument()
ReturnEndIfDim storage = New DiagramStorage()
Dim diagramInfo = storage.DiagramData.FirstOrDefault(Function(x) x.Name = CStr(e.DocumentSource))
If diagramInfo IsNotNothingThen
diagram.LoadDocument(New MemoryStream(diagramInfo.Data))
EndIf
e.Handled = TrueEndSubPrivateSub OnShowingSaveDialog(ByVal sender AsObject, ByVal e As DevExpress.Xpf.Diagram.DiagramShowingSaveDialogEventArgs)
Dim viewModel = SelectDiagramViewModel.Create()
viewModel.SelectedName = CStr(diagram.DocumentSource)
Dim result = saveDialogService.ShowDialog(MessageButton.OKCancel, "Choose a save location", viewModel)
If result = MessageResult.OK Then
e.DocumentSourceToSave = viewModel.SelectedName
Else
e.Cancel = TrueEndIfEndSubPrivateSub OnCustomSaveDocument(ByVal sender AsObject, ByVal e As DevExpress.Xpf.Diagram.DiagramCustomSaveDocumentEventArgs)
Dim storage = New DiagramStorage()
Dim diagramInfo = storage.DiagramData.FirstOrDefault(Function(x) x.Name = CStr(e.DocumentSource))
If diagramInfo IsNothingThen
diagramInfo = New DiagramData() With {.Name = CStr(e.DocumentSource)}
storage.DiagramData.Add(diagramInfo)
EndIfDim stream = New MemoryStream()
diagram.SaveDocument(stream)
diagramInfo.Data = stream.ToArray()
storage.SaveChanges()
e.Handled = TrueEndSubEndClassPublicClass SelectDiagramViewModel
PublicSharedFunction Create() As SelectDiagramViewModel
Return ViewModelSource.Create(Function() New SelectDiagramViewModel())
EndFunctionProtectedSubNew()
Dim storage = New DiagramStorage()
Names = storage.DiagramData.Select(Function(x) x.Name).ToArray()
SelectedName = Names.FirstOrDefault()
EndSubPrivate privateNames AsString()
PublicProperty Names() AsString()
GetReturn privateNames
EndGetPrivateSet(ByVal value AsString())
privateNames = value
EndSetEndPropertyPublicOverridableProperty SelectedName() AsStringEndClassEndNamespace
usingDevExpress.Diagram.Core;
usingDevExpress.Xpf.Core;
usingDevExpress.Xpf.Diagram;
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Data.Entity;
usingSystem.IO;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Threading.Tasks;
usingSystem.Windows;
namespaceDXDiagram.CustomDiagramStorage {
classDiagramStorageInitializer : DropCreateDatabaseIfModelChanges<DiagramStorage> {
protectedoverridevoidSeed(DiagramStorage storage) {
base.Seed(storage);
var diagram = new DiagramControl();
for(int i = 0; i < 5; i++) {
diagram.Items.Add(new DiagramShape() {
Position = new Point(200, 100 + i * 100),
Width = 100,
Height = 50,
Content = "Item " + (i + 1).ToString(),
});
if(i == 0)
continue;
using(var stream = new MemoryStream()) {
diagram.SaveDocument(stream);
var diagramData = new DiagramData() {
Name = (i + 1).ToString() + " items",
Data = stream.ToArray(),
};
storage.DiagramData.Add(diagramData);
}
}
storage.SaveChanges();
}
}
}
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Windows;
usingSystem.Windows.Controls;
usingSystem.Windows.Data;
usingSystem.Windows.Documents;
usingSystem.Windows.Input;
usingSystem.Windows.Media;
usingSystem.Windows.Media.Imaging;
usingSystem.Windows.Navigation;
usingSystem.Windows.Shapes;
usingDevExpress.Xpf.Core;
usingDevExpress.Diagram.Core;
usingDevExpress.Mvvm;
using DevExpress.Mvvm.POCO;
usingSystem.IO;
namespaceDXDiagram.CustomDiagramStorage {
///<summary>/// Interaction logic for MainWindow.xaml///</summary>publicpartialclassMainWindow : DXWindow {
publicMainWindow() {
InitializeComponent();
}
privatevoidOnLoaded(object sender, RoutedEventArgs e) {
Dispatcher.BeginInvoke(new Action(diagram.OpenFile));
}
privatevoidOnShowingOpenDialog(object sender, DevExpress.Xpf.Diagram.DiagramShowingOpenDialogEventArgs e) {
var viewModel = SelectDiagramViewModel.Create();
var result = openDialogService.ShowDialog(MessageButton.OKCancel, "Choose a diagram to open", viewModel);
if(result == MessageResult.OK)
e.DocumentSourceToOpen = viewModel.SelectedName;
else
e.Cancel = true;
}
privatevoidOnCustomLoadDocument(object sender, DevExpress.Xpf.Diagram.DiagramCustomLoadDocumentEventArgs e) {
if(e.DocumentSource == null) {
diagram.NewDocument();
return;
}
var storage = new DiagramStorage();
var diagramInfo = storage.DiagramData.FirstOrDefault(x => x.Name == (string)e.DocumentSource);
if(diagramInfo != null)
diagram.LoadDocument(new MemoryStream(diagramInfo.Data));
e.Handled = true;
}
privatevoidOnShowingSaveDialog(object sender, DevExpress.Xpf.Diagram.DiagramShowingSaveDialogEventArgs e) {
var viewModel = SelectDiagramViewModel.Create();
viewModel.SelectedName = (string)diagram.DocumentSource;
var result = saveDialogService.ShowDialog(MessageButton.OKCancel, "Choose a save location", viewModel);
if(result == MessageResult.OK)
e.DocumentSourceToSave = viewModel.SelectedName;
else
e.Cancel = true;
}
privatevoidOnCustomSaveDocument(object sender, DevExpress.Xpf.Diagram.DiagramCustomSaveDocumentEventArgs e) {
var storage = new DiagramStorage();
var diagramInfo = storage.DiagramData.FirstOrDefault(x => x.Name == (string)e.DocumentSource);
if(diagramInfo == null) {
diagramInfo = new DiagramData() { Name = (string)e.DocumentSource };
storage.DiagramData.Add(diagramInfo);
}
var stream = new MemoryStream();
diagram.SaveDocument(stream);
diagramInfo.Data = stream.ToArray();
storage.SaveChanges();
e.Handled = true;
}
}
publicclassSelectDiagramViewModel {
publicstatic SelectDiagramViewModel Create() {
return ViewModelSource.Create(() => new SelectDiagramViewModel());
}
protectedSelectDiagramViewModel() {
var storage = new DiagramStorage();
Names = storage.DiagramData.Select(x => x.Name).ToArray();
SelectedName = Names.FirstOrDefault();
}
publicstring[] Names { get; privateset; }
publicvirtualstring SelectedName { get; set; }
}
}
If you have any questions, submit a ticket to our Support Center.
No
Your feedback is appreciated.
By clicking “Accept All Cookies”, you agree to the storing of cookies on your device to enhance site navigation, analyze site usage, and assist in our marketing efforts.
Privacy Preference Center
When you visit a Developer Express Inc (“DevExpress”) website, it may store or retrieve information on your browser, mostly in the form of cookies. This information might be about you, your preferences or your device and is mostly used to make the site work as you expect it to. While the information does not usually directly identify you, it can give you a more personalized web experience. Because DevExpress respects your right to privacy, you can choose to disallow/disable the use of certain cookies. Click on different category headings to learn more and change our default settings. Keep in mind that blocking some types of cookies may impact your experience on the site and may affect the services DevExpress is able to offer to you. You cannot opt-out of our use of strictly necessary cookies as they are used to ensure the proper functioning of our Websites (such as remembering your settings, allowing you to log into your account, and other similar purposes). You may, however, opt-out of receiving and our use of non-essential cookies (including preference, functional, and targeting cookies) by changing your settings for each category listed below.
[Videos]
Our use of cookies may also collect information about what videos you have watched on our websites. You may opt-out of these cookies by changing your settings for functional and advertising cookies. We will ask you to review and update your choices at least once every two (2) years. By continuing to allow us to use these cookies you explicitly consent to our use of cookies and our disclosure of what videos you have watched on our Websites to our video hosting providers, such as YouTube, for a period of up to two (2) years.
Manage Consent Preferences
Strictly Necessary Cookies
Always Active
These cookies are necessary for the website to function properly and cannot be disabled. They are usually set in response to actions initiated by you – actions that amount to a request for services, such as setting your privacy preferences, logging onto the website, or populating website forms. You can set your browser to block or alert you about these cookies, but certain portions of the site will not work properly when these cookies are disabled. These cookies do not store any personally identifiable information.
Performance Cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand page popularity and determine how visitors move around the site. All information collected by these cookies are aggregated and therefore anonymous. If you disallow/disable these cookies, we will not know when you have visited our site and we will not be able to monitor its performance.
Functional Cookies
These cookies allow the website to provide enhanced functionality and personalization. They may be set by us or by third party providers whose services we have added to our pages. If you disallow/disable these cookies, some or all of these services may fail to function properly.
Targeting Cookies
These cookies may be set through our site by our advertising partners. They may be used by advertising partners to build a profile of your interests and display relevant advertisements on other sites. While these cookies do not store personal information, they do identify your browser and internet device. If you disallow/disable these cookies, you will experience less targeted advertising.