Abbas
Abbas

Reputation: 5044

How to set the style in WPF Window.Resources.

I want to create multiple styles in the Window.Resources. Below is the code I tried, but it's not working:

<Window.Resources>
    <Style x:Key="StyleOne" TargetType="{x:Type Control}">
        <Setter Property="Control.Background" Value="Blue"></Setter>
        <Setter Property="Control.Height" Value="20"></Setter>
    </Style>
    <Style x:Key="StyleTwo" BasedOn="{StaticResource StyleOne}">
        <Setter Property="Control.Background" Value="Red"></Setter>
        <Setter Property="Control.Height" Value="20"></Setter>
    </Style>
</Window.Resources>
<Button Style="{StaticResource StyleOne}"></Button>
<Button Style="{StaticResource StyleTwo}"></Button>

It throws an error saying:

The property "Content" is set more than once.

Upvotes: 6

Views: 13910

Answers (3)

brunnerh
brunnerh

Reputation: 184296

This error has nothing to do with styles, the window can only contain one child (which sets the Content), use some container which can contain more than one child. e.g. a StackPanel or Grid.

<StackPanel>
     <Button .../>
     <Button .../>
</StackPanel>

(See also: Panels Overview)

Upvotes: 7

Kishore Kumar
Kishore Kumar

Reputation: 21863

set the target type for the second style

 <Style x:Key="StyleTwo"
           BasedOn="{StaticResource StyleOne}"
           TargetType="{x:Type Control}">
        <Setter Property="Control.Background"
                Value="Red"></Setter>
        <Setter Property="Control.Height"
                Value="20"></Setter>
    </Style>

put the buttons inside a stackpanel or Grid

Upvotes: 5

DotNetUser
DotNetUser

Reputation: 6612

I guess BasedOn inherits the properties from other style type and you have

    Property="Control.Background"

set in both the styles hence getting an error

    "The property "Content" is set more than once."

Upvotes: -2

Related Questions