P
P
pimanov32021-12-04 07:58:13
WPF
pimanov3, 2021-12-04 07:58:13

How to set properties on child elements?

There is a need to set the text color and padding for each child element of the TreeView. How can this be done without assigning these properties to each element separately? WPF project.

<TreeView Padding="0,3,0,0" Background="#FF09304A">
    <Label Content="Формула 1" Foreground="White" Margin="10"/>
    <Label Content="Формула 2" Foreground="White" Margin="10"/>
    <Label Content="Формула 3" Foreground="White" Margin="10"/>
</TreeView>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sumor, 2021-12-04
@pimanov3

You can set general styles for elements in resources.
In this case, you can apply your styles to all types of elements or explicitly specify the name.
In the simplest case, it looks like this:

<TreeView Padding="0,3,0,0" Background="#FF09304A">
<TreeView.Resources>
<Style Type="Label">
<Setter Property="Foreground" Value="White" />
<Setter Property="Margin" Value="10" />
</Style>
</TreeView.Resources>
    <Label Content="Формула 1" />
    <Label Content="Формула 2" />
    <Label Content="Формула 3" />
</TreeView>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question