WPF View is giving Error
<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-compati开发者_开发百科bility/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.
You could try adding x:Shared="False" to the declaration of the MyToolbar resource. This will cause the runtime to always create a new instance of the resource rather than reusing an existing one.
The XAML that you are using is not designed to ever be loaded twice. The line that causes the error is a ContentControl
setting its Content
property to a StaticResource
. As you say, you are loading it twice so there are two instances of the ContentControl
but 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:
Specified element is already the logical child of another element
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.
Rick is correct.
I had exactly the same problem and used the following to work around it.
Basically when you navigate to the page, keep a record of that page, and then if you try to navigate to the page again, first check if you are navigated to the page. If you are on the page already, do nothing, otherwise navigate to the page.
public partial class MainWindow : Window
{
Uri currentPage;
private void btnNavigateToPage(object sender, MouseButtonEventArgs e)
{
if (currentPage == null)
{
this.Cursor = Cursors.Wait;
frameWorkingArea.Navigate(new Uri("/pgMyPage.xaml", UriKind.RelativeOrAbsolute));
currentPage = frameWorkingArea.Source;
}
if (!currentPage.Equals("/pgMyPage.xaml"))
{
frameWorkingArea.Navigate(new Uri("/pgMyPage.xaml", UriKind.RelativeOrAbsolute));
currentPage = frameWorkingArea.Source;
}
}
}
Hope that helps.
Regards, Neill
精彩评论