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.
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)
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With