Save one file from multiple Tab Items

anewuser 1 Reputation point
2022-02-22T10:03:40.203+00:00

I have a WPF program with 5 tab items, each of which are a Canvas. I can drag and drop various items onto each Canvas. Currently, when I execute my Save function, only the selected tab is saved. How can I iterate through all the tabs containing canvases and save them at the same time? Is there a way I can make a list of Canvases?

Many thanks.

Edit to add code:
I am using this code as my base: https://www.codeproject.com/script/Articles/ViewDownloads.aspx?aid=24681

The Save command used is under DesignerCanvas.Commands.cs (Save_Executed). For your reference here:

private void Save_Executed(object sender, ExecutedRoutedEventArgs e)
{
              IEnumerable<DesignerItem> designerItems = this.Children.OfType<DesignerItem>();
              IEnumerable<Connection> connections = this.Children.OfType<Connection>();

              XElement designerItemsXML = SerializeDesignerItems(designerItems);
              XElement connectionsXML = SerializeConnections(connections);

              XElement root = new XElement("Root");
              root.Add(designerItemsXML);
              root.Add(connectionsXML);

              SaveFile(root);          
}

Window1.xaml is the same except the tabs are inserted in the <!--Designer--> as follows:

<GroupBox Header="Diagram" Grid.Column="1" Margin="3,0,3,3" Style="{StaticResource ToolbarGroup}" x:Name="MyCanvas">
                    <TabControl Name="MyTabs" >
                        <TabItem Header="Tab 1" Focusable="True">
                            <ScrollViewer HorizontalScrollBarVisibility="Auto"
                                 VerticalScrollBarVisibility="Auto">
                                <s:DesignerCanvas Focusable="true" x:Name="MyDesigner" 
                                    Background="{StaticResource WindowBackgroundBrush}"
                                    Margin="0" 
                                    ContextMenu="{StaticResource DesignerCanvasContextMenu}"/>
                             </ScrollViewer>
                        </TabItem>
                        <TabItem Header="Tab 2" Focusable="True">
                        <ScrollViewer HorizontalScrollBarVisibility="Auto"
                                 VerticalScrollBarVisibility="Auto">
                            <s:DesignerCanvas Focusable="true" x:Name="MyDesigner2"
                                    Background="{StaticResource WindowBackgroundBrush}"
                                    Margin="0" 
                                    ContextMenu="{StaticResource DesignerCanvasContextMenu}"/>
                        </ScrollViewer>
                    </TabControl>
            </GroupBox>

I believe it would be better to use binding for the header and content of each tab, but I am not sure how I can bind DesignerCanvas class to the tab content, hence why I manually added each tab. I have only added two tabs here for simplicity.

Thank you!

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,762 questions
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.