1

Based on the below xaml, does anybody know why if I create 2 (or more) buttons (with implicit styles), only 1 of them actually displays the content, however all display the blue border.

<Style TargetType="Button">

  <Setter Property="Template">
    <Setter.Value>
      <ControlTemplate TargetType="Button">
        <Border BorderBrush="{TemplateBinding BorderBrush}"
                BorderThickness="1">
          <ContentPresenter Content="{TemplateBinding Content}"/>
        </Border>
      </ControlTemplate>
    </Setter.Value>
  </Setter>

  <Setter Property="BorderBrush" Value="Blue"/>

  <Setter Property="Content">
    <Setter.Value>
      <Grid Background="Firebrick"/>
    </Setter.Value>
  </Setter>

</Style>

Checking the code behind, all 3 buttons have the exact same content, yet it is only ever displayed on 1 of them

if (a.Style == b.Style && a.Style == c.Style)
{
    if (a.Content == b.Content && a.Content == c.Content)
    {
        // comes in here!
    }
}

enter image description here

Massimiliano Kraus
  • 3,638
  • 5
  • 27
  • 47
the.Doc
  • 867
  • 9
  • 17
  • 2
    all 3 buttons have the exact same content, but UIElement cannot belong to more than one parent and therefore displayed in last of button. consider making Grid a part of Template – ASh Oct 19 '16 at 11:10
  • 2
    Take a look at the fourth bullet in the Remarks section of the [ContentPresenter](https://msdn.microsoft.com/en-us/library/system.windows.controls.contentpresenter(v=vs.110).aspx) page on MSDN: *If Content is a UIElement object, the UIElement is displayed. If the UIElement already has a parent, an exception occurs*. However, apparently no exception occurs, or it is caught internally. – Clemens Oct 19 '16 at 11:14
  • Very frustrating, thought this might have been the issue but confused me as no exception is raised. – the.Doc Oct 19 '16 at 11:22
  • Possible duplicate of [Set a Button's content in style](http://stackoverflow.com/questions/7250709/set-a-buttons-content-in-style) – the.Doc Oct 24 '16 at 20:33

2 Answers2

1

The problem, as mentioned by Clemens and ASh is the Grid in the Content cannot have more than 1 parent.

The workaround is to use a DataTemplate and set the element's ContentTemplate as the DataTemplate (as described in the thread below).

Set a Button's content in style

Community
  • 1
  • 1
the.Doc
  • 867
  • 9
  • 17
0

I already faced this issue with button images. This is linked to your style where you defined the Content property and not the Template property. By setting the Content property you don't define a reference to something nor you ask WPF to create a new object for every item, meaning only the last element rendered with the style will have is Content set.

So what you want to do is either, set the ContentTemplate property if you planned to set a content to every buttons. Or set the Template property if you actually only want to change the aspect of the Button.

For example with the Template property:

<Setter Property="Template">
    <Setter.Value>
        <ControlTemplate TargetType="Button">
            <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="1">
                <ContentPresenter Content="{TemplateBinding Content}" />
            </Border>
        </ControlTemplate>
    </Setter.Value>
</Setter>

<Setter Property="BorderBrush" Value="Blue" />

<Setter Property="Template">
    <Setter.Value>
        <ControlTemplate TargetType="{x:Type Button}">
            <Grid Background="Firebrick" />
        </ControlTemplate>
    </Setter.Value>
</Setter>

Amaury Levé
  • 1,459
  • 10
  • 21