How to set style in WPF Window.Resources.

I want to create some styles in Window.Resources. Below is the code I tried, but it does not work:

<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 gives an error message:

The Content property is set more than once.

+4
source share
3 answers

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

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

(See also: Panel Overview )

+6
source

set the goal 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> 

place buttons inside the stack or grid panel

+4
source

I think BasedOn inherits properties from another type of style, and you

  Property="Control.Background" 

set in both styles, therefore, gets an error

  "The property "Content" is set more than once." 
-2
source

Source: https://habr.com/ru/post/1395518/


All Articles