<Page x:Class="Project.ProjectDiagramView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:s="clr-namespace:DiagramDesigner"
xmlns:c="clr-namespace:DiagramDesigner.Controls"
mc:Ignorable="d"
d:DesignHeight="850" d:DesignWidth="1000"
Title="Project Diagram">
<Grid Margin="10">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<ContentControl Content="{StaticResource MyToolbar}"/>
<Grid Grid.Row="1" Margin="0,10,0,0">
<Grid.ColumnDefinitions>
<ColumnDefinition MinWidth="135" MaxWidth="135"/>
<ColumnDefinition Width="*" />
<ColumnDefinition MaxWidth="500" MinWidth="350" />
</Grid.ColumnDefinitions>
<!-- Toolbox -->
<StackPanel Grid.Column="0" Margin="0,0,5,0">
<!--<Expander Header="Symbols" Content="{StaticResource SymbolStencils}" IsExpanded="True"/>-->
</StackPanel>
<!-- GridSplitter -->
<GridSplitter Focusable="False" Width="2" Background="LightGray"
VerticalAlignment="Stretch" HorizontalAlignment="Right"/>
<!-- Designer -->
<GroupBox Header="Diagram" Grid.Column="1" Margin="3,0,3,0">
<ScrollViewer HorizontalScrollBarVisibility="Auto"
VerticalScrollBarVisibility="Auto">
<s:DesignerCanvas Focusable="true" x:Name="MyDesigner"
Background="{StaticResource WindowBackgroundBrush}"
Margin="10" FocusVisualStyle="{x:Null}" />
</ScrollViewer>
</GroupBox>
<GroupBox Header="Diagram" Grid.Column="3" Margin="3,0,0,0">
<ScrollViewer HorizontalScrollBarVisibility="Auto"
VerticalScrollBarVisibility="Auto">
<s:SelectedDesignItem />
</ScrollViewer>
</GroupBox>
</Grid>
</Grid>
</Page>
This is the Diagram Designer i am using from codeproject, the above code gives error second time it is loaded
‘Set property ‘System.Windows.Controls.ContentControl.Content’ threw an exception.’ Line number ’24’ and line position ’10’.
first time when the page is loaded, works perfectly, but navigating to the same page second time it thorws error, i am clueless about the error
I have tried to comment the toolbox, it works perfectly then, is toolbox not getting disposed, i dont know.
please can any one guide me.
Thank you.
The XAML that you are using is not designed to ever be loaded twice. The line that causes the error is a
ContentControlsetting itsContentproperty to aStaticResource. As you say, you are loading it twice so there are two instances of theContentControlbut there is only one instance of the static resource. WPF will not allow the same element to belong to two different visual trees and therefore on the second load you get the error message, I believe:To fix this problem would require a fair amount of rework. One approach is to convert the direct static content of the content control into a template, but without knowing how the application is structured, it’s hard to say if this would work easily or just create new problems.