Skip to main content

DiagramControl.ShowingOpenDialog Event

Fires before showing an ‘Open File’ dialog, and allows it to be customized.

Namespace: DevExpress.Xpf.Diagram

Assembly: DevExpress.Xpf.Diagram.v24.1.dll

NuGet Package: DevExpress.Wpf.Diagram

Declaration

public event EventHandler<DiagramShowingOpenDialogEventArgs> ShowingOpenDialog

Event Data

The ShowingOpenDialog event's data class is DiagramShowingOpenDialogEventArgs. The following properties provide information specific to this event:

Property Description
Cancel Gets or sets whether the event should be canceled. Inherited from CancelRoutedEventArgs.
DocumentSourceToOpen Gets or sets the object from which to load the diagram skipping the dialog window. This object can be a full file path, Uri object, stream or array of bytes.
Filter Gets or sets the string that determines the file filtering options that appear in the dialog.
Handled Gets or sets a value that indicates the present state of the event handling for a routed event as it travels the route. Inherited from RoutedEventArgs.
InitialDirectory Gets or sets the path to the directory displayed by the dialog.
OriginalSource Gets the original reporting source as determined by pure hit testing, before any possible Source adjustment by a parent class. Inherited from RoutedEventArgs.
RoutedEvent Gets or sets the RoutedEvent associated with this RoutedEventArgs instance. Inherited from RoutedEventArgs.
Source Gets or sets a reference to the object that raised the event. Inherited from RoutedEventArgs.
Title Gets or sets the title of the dialog.

The event data class exposes the following methods:

Method Description
InvokeEventHandler(Delegate, Object) When overridden in a derived class, provides a way to invoke event handlers in a type-specific way, which can increase efficiency over the base implementation. Inherited from RoutedEventArgs.
OnSetSource(Object) When overridden in a derived class, provides a notification callback entry point whenever the value of the Source property of an instance changes. Inherited from RoutedEventArgs.

Remarks

This event allows you to respond to an ‘Open File’ dialog being invoked. You can handle it to change the dialog or prevent it from being displayed. To prevent the dialog from being displayed, set the event’s Cancel parameter to true.

You can also use the following event’s options:

  • DocumentSourceToOpen - allows you to specify the object that contains the diagram.
  • Filter - allows you to specify the current file name filter string which determines the files that are displayed by the dialog box.
  • InitialDirectory - allows you to specify the initial directory displayed by the dialog box.
  • Title - allows you to specify the dialog box title.

Example

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.

<dx:DXWindow x:Class="DXDiagram.CustomDiagramStorage.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:dx="http://schemas.devexpress.com/winfx/2008/xaml/core"
        xmlns:dxmvvm="http://schemas.devexpress.com/winfx/2008/xaml/mvvm"
        xmlns:dxb="http://schemas.devexpress.com/winfx/2008/xaml/bars"
        xmlns:dxe="http://schemas.devexpress.com/winfx/2008/xaml/editors"
        xmlns:dxdiag="http://schemas.devexpress.com/winfx/2008/xaml/diagram"
        Title="MainWindow" Height="800" Width="1400" Loaded="OnLoaded" WindowStartupLocation="CenterScreen">
    <Window.Resources>
        <Style x:Key="DialogWindowStyle" TargetType="dx:DXWindow">
            <Setter Property="WindowStyle" Value="ToolWindow"/>
            <Setter Property="Width" Value="250"/>
            <Setter Property="Height" Value="300"/>
        </Style>
    </Window.Resources>
    <dxmvvm:Interaction.Behaviors>
        <dx:DialogService Name="openDialogService" DialogWindowStartupLocation="CenterScreen" DialogStyle="{StaticResource DialogWindowStyle}">
            <dx:DialogService.ViewTemplate>
                <DataTemplate>
                    <ListBox ItemsSource="{Binding Names}" SelectedItem="{Binding SelectedName}"/>
                </DataTemplate>
            </dx:DialogService.ViewTemplate>
        </dx:DialogService>
        <dx:DialogService Name="saveDialogService" DialogWindowStartupLocation="CenterScreen" DialogStyle="{StaticResource DialogWindowStyle}">
            <dx:DialogService.ViewTemplate>
                <DataTemplate>
                    <Grid>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="*"/>
                        </Grid.RowDefinitions>
                        <dxe:TextEdit Text="{Binding SelectedName, UpdateSourceTrigger=PropertyChanged}"/>
                        <ListBox Grid.Row="1" ItemsSource="{Binding Names}" SelectedItem="{Binding SelectedName}"/>
                    </Grid>
                </DataTemplate>
            </dx:DialogService.ViewTemplate>
        </dx:DialogService>
    </dxmvvm:Interaction.Behaviors>
    <Grid>
        <dxdiag:DiagramDesignerControl x:Name="diagram" 
                                       SelectedStencils="BasicShapes,ArrowShapes"
                                       ShowingOpenDialog="OnShowingOpenDialog"
                                       CustomLoadDocument="OnCustomLoadDocument"
                                       ShowingSaveDialog="OnShowingSaveDialog"
                                       CustomSaveDocument="OnCustomSaveDocument"/>
    </Grid>
</dx:DXWindow>
See Also