Search code examples
.netwpfxaml.net-4.0popup

Error: Cannot find the Style Property 'Template' on the type 'System.Windows.Controls.Primitives.Popup'


I have to create style for popups. I am using WPF and .NET Framework 4.

I have written style:

<Style x:Key="PopupBox" TargetType="{x:Type Popup}">   
    <Setter Property="OverridesDefaultStyle" Value="True" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Popup}">       
                <Grid>  
                    <Border BorderBrush="Blue" Background="#FFFFFFFF">
                        <Grid>                                
                            <Border Background="AliceBlue"/>  
                            <ContentPresenter ContentSource="Header" /> 
                            <ContentPresenter/>
                            <Border BorderBrush="#FFFFFFFF" Background="#FFBFDBFF"/>
                        </Grid>
                    </Border>
                </Grid>                    
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style> 

I have cut off from this code some elements like grid row and column definitions because they are not significant.

So it seems that I can't use <Setter Property="Template"> because Popup control doesn't have this property. How can I work around this?

Any help here much appreciated!


Solution

  • As Popup doesn`t have any template and just a Child property for content, you can use some other control (for example ContentControl) to style and template:

    <Style x:Key="PopupContentStyle" TargetType="ContentControl">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="ContentControl">
                    <Border BorderBrush="Blue" Background="#FFFFFFFF">
                        <Grid>                                
                            <Border Background="AliceBlue"/>  
                            <ContentPresenter/>
                            <Border BorderBrush="#FFFFFFFF" Background="#FFBFDBFF"/>
                        </Grid>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
         </Setter>
    </Style>
    

    Then just place it in every Popup that needs it:

    <Popup>
        <ContentControl Style="{StaticResource PopupContentStyle}">
           <!-- Some content here -->
        </ContentControl>
    </Popup>