I have a button style where I have some Path seth in Content property. That is working fine until I display a second instance of this button. I am getting an exception
Specified element is already the logical child of another element. Disconnect it first.
Other posts like this:
Error "Specified element is already the logical child of another element"?
led me to a solution that I need to transfer Content to ContentTemplate.
<Viewbox>
<Grid Margin="0,0,30,30">
<Path Fill="#FFFFFFFF">
<Path.Data>
<PathGeometry Figures="m 13.123027 65.796864 0 81.448876 133.750213 0 0 -133.778725 -67.192062 0 z" FillRule="NonZero"/>
</Path.Data>
</Path>
<Path Fill="{StaticResource DataCRUDIconBrush}">
<Path.Data>
<PathGeometry Figures="M 79.624708 0.36218262 0 62.950511 l 0 97.411669 160 0 0 -159.99999738 -80.375292 0 z m 2.28303 16.89635038 61.172792 0 0 126.207297 -126.161061 0 0 -76.829978 0.187646 -0.156158 64.800623 0 0 -49.221161 z" FillRule="NonZero"/>
</Path.Data>
</Path>
<Path Fill="#FFFFFFFF">
<Path.Data>
<PathGeometry Figures="m 13.123027 65.796864 0 81.448876 133.750213 0 0 -133.778725 -67.192062 0 z" FillRule="NonZero"/>
</Path.Data>
<Path.RenderTransform>
<TranslateTransform X="30" Y="30"/>
</Path.RenderTransform>
</Path>
<Path Fill="{StaticResource DataCRUDIconBrush}">
<Path.Data>
<PathGeometry Figures="M 79.624708 0.36218262 0 62.950511 l 0 97.411669 160 0 0 -159.99999738 -80.375292 0 z m 2.28303 16.89635038 61.172792 0 0 126.207297 -126.161061 0 0 -76.829978 0.187646 -0.156158 64.800623 0 0 -49.221161 z" FillRule="NonZero"/>
</Path.Data>
<Path.RenderTransform>
<TranslateTransform X="30" Y="30"/>
</Path.RenderTransform>
</Path>
</Grid>
</Viewbox>
How can I translate this code to fit the ContentTemplate, without loosing the triggers I hava there?
<Setter.Value>
<ControlTemplate TargetType="Button">
....
<ContentPresenter x:Name="Content" Opacity="0.5" Content="{TemplateBinding Content}" ContentTemplate="{TemplateBinding ContentTemplate}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" />
</ControlTemplate>
</Setter.Value>
I know your problem. You are using data binding for the
ContentPresenter.Contentproperty.And in this case, you need to be sure, that your binding object is not a visual element. It is very important. Disconnection from logical tree is not a good solution (just ‘bottle-necks’, another words).
Content can contains only non-visual objects. All visual parts of your control should be in its
ContentPresenter.ContentTemplateproperty and no another way.Thus I think all your code with graphics should be placed in the ContentTemplate property. If you have troubles with it, please, share your entire sample and I try to help you, as much i can.