I have a WPF application running without any issues in my windows 8 OS development machine. But when i try to run the same in Windows 7 machine i am getting the error
System.InvalidOperationException: Specified element is already the logical child of another element. Disconnect it first.
The detailed error log is
[CDATA[Set property 'System.Windows.FrameworkElement.Style' threw an exception.
LoadBaml at offset 481 in file:line:column <filename unknown>:0:0
System.InvalidOperationException: Specified element is already the logical child of another element. Disconnect it first.
at System.Windows.FrameworkElement.ChangeLogicalParent(DependencyObject newParent)
at System.Windows.FrameworkElement.AddLogicalChild(Object child)
at System.Windows.Controls.ContentControl.OnContentChanged(Object oldContent, Object newContent)
at System.Windows.Controls.ContentControl.OnContentChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
at System.Windows.DependencyObject.OnPropertyChanged(DependencyPropertyChangedEventArgs e)
at System.Windows.FrameworkElement.OnPropertyChanged(DependencyPropertyChangedEventArgs e)
at System.Windows.DependencyObject.NotifyPropertyChange(DependencyPropertyChangedEventArgs args)
at System.Windows.DependencyObject.UpdateEffectiveValue(EntryIndex entryIndex, DependencyProperty dp, PropertyMetadata metadata, EffectiveValueEntry oldEntry, EffectiveValueEntry& newEntry, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType)
at System.Windows.StyleHelper.ApplyStyleOrTemplateValue(FrameworkObject fo, DependencyProperty dp)
at System.Windows.StyleHelper.InvalidateContainerDependents(DependencyObject container, FrugalStructList`1& exclusionContainerDependents, FrugalStructList`1& oldContainerDependents, FrugalStructList`1& newContainerDependents)
at System.Windows.StyleHelper.DoStyleInvalidations(FrameworkElement fe, FrameworkContentElement fce, Style oldStyle, Style newStyle)
at System.Windows.StyleHelper.UpdateStyleCache(FrameworkElement fe, FrameworkContentElement fce, Style oldStyle, Style newStyle, Style& styleCache)
at System.Windows.FrameworkElement.OnStyleChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
at System.Windows.DependencyObject.OnPropertyChanged(DependencyPropertyChangedEventArgs e)
at System.Windows.FrameworkElement.OnPropertyChanged(DependencyPropertyChangedEventArgs e)
at System.Windows.DependencyObject.NotifyPropertyChange(DependencyPropertyChangedEventArgs args)
at System.Windows.DependencyObject.UpdateEffectiveValue(EntryIndex entryIndex, DependencyProperty dp, PropertyMetadata metadata, EffectiveValueEntry oldEntry, EffectiveValueEntry& newEntry, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType)
at System.Windows.DependencyObject.SetValueCommon(DependencyProperty dp, Object value, PropertyMetadata metadata, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType, Boolean isInternal)
at System.Windows.DependencyObject.SetValue(DependencyProperty dp, Object value)
at System.Windows.Baml2006.WpfKnownMemberInvoker.SetValue(Object instance, Object value)
at MS.Internal.Xaml.Runtime.ClrObjectRuntime.SetValue(XamlMember member, Object obj, Object value)
at MS.Internal.Xaml.Runtime.ClrObjectRuntime.SetValue(Object inst, XamlMember property, Object value)]]
I have no idea which element XAML or code behind causing this issue. Any help to find out reason for this strange issue?
Finally able to find the reason for error I have a ToggleButton style in the application which is defined in App.xaml like this
<Style x:Key="ONStateLabelLib" TargetType="Label">
<Setter Property="Background" Value="#A1A1A1"></Setter>
<Setter Property="Foreground" Value="Black"></Setter>
<Setter Property="Margin" Value="0"></Setter>
<Setter Property="Padding" Value="0"></Setter>
<Setter Property="Cursor" Value="Hand"/>
<Setter Property="Width" Value="45"></Setter>
<Setter Property="VerticalContentAlignment" Value="Center"></Setter>
<Setter Property="HorizontalContentAlignment" Value="Center"></Setter>
<Setter Property="FontSize" Value="14"/>
<Setter Property="FontFamily" Value="Arial"/>
</Style>
<Style x:Key="OFFStateLabelLib" TargetType="Label">
<Setter Property="Cursor" Value="Hand"/>
<Setter Property="Margin" Value="0"></Setter>
<Setter Property="Padding" Value="0"></Setter>
<Setter Property="FontSize" Value="14"/>
<Setter Property="FontFamily" Value="Arial"/>
<Setter Property="FontWeight" Value="ExtraLight"/>
<Setter Property="Foreground" Value="#515151"></Setter>
<Setter Property="Background" Value="#393939"></Setter>
<Setter Property="Width" Value="45"></Setter>
<Setter Property="BorderBrush" Value="#A1A1A1"></Setter>
<Setter Property="BorderThickness" Value="2"></Setter>
<Setter Property="VerticalContentAlignment" Value="Center"></Setter>
<Setter Property="HorizontalContentAlignment" Value="Center"></Setter>
</Style>
<Style x:Key="OnOffToggleStyleLib" TargetType="ToggleButton" >
<Setter Property="VerticalAlignment" Value="Center"></Setter>
<Setter Property="Width" Value="90"></Setter>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ToggleButton">
<ContentPresenter VerticalAlignment="Center" />
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="Transparent">
</Setter>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsChecked" Value="True">
<Setter Property="Content">
<Setter.Value>
<DockPanel Margin="0">
<Label DockPanel.Dock="Left" Style="{StaticResource ResourceKey=ONStateLabelLib}" >YES</Label>
<Label Style="{StaticResource ResourceKey=OFFStateLabelLib}" >NO</Label>
</DockPanel>
</Setter.Value>
</Setter>
</Trigger>
<Trigger Property="IsChecked" Value="False">
<Setter Property="Content">
<Setter.Value>
<DockPanel Margin="0">
<Label DockPanel.Dock="Left" Style="{StaticResource ResourceKey=OFFStateLabelLib}">YES</Label>
<Label Style="{StaticResource ResourceKey=ONStateLabelLib}" >NO</Label>
</DockPanel>
</Setter.Value>
</Setter>
</Trigger>
</Style.Triggers>
</Style>
And i use the Toggle Button in 2 pages Page1.xaml and Page2.xaml like this
<ToggleButton Margin="5" Grid.Row="0"
ToolTip="{Binding Path=localResource.reader_tooltip_settings_fullscreen,Source={StaticResource DbookLanguageManagerDynamic}}" Grid.Column="1"
Name="btn_fullScreen" Click="btn_fullScreen_Clicked"
Style="{StaticResource ResourceKey=OnOffToggleStyleLib}" />
Application Loads Page1.xaml first and While Navigating to Page2.xaml i ma getting above exception .
Now can some one help to solve this?
Try changing the
Content
property setter toContentTemplate
, and using aDataTemplate
as value:I also swapped the
DockPanel
with aStackPanel
, it's a better approach in my opinion. Let me know if it works :)