D
D
Denis Mashanov2017-09-07 13:56:20
WPF
Denis Mashanov, 2017-09-07 13:56:20

How to create a common property for elements?

Good evening everyone. Please give an example of a common property for elements. For example, I have many elements, and they have the "Width" property, so that for each the same width number is not written, how can I make a general one? That is, so that all elements refer to one line.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexey Pavlov, 2017-09-07
@LoneRay

You must specify the type of elements that you want to style. In a DataGrid, the cells are in the DataGridCell element and the headers are in the DataGridColumnHeader.

<DataGrid>
    <DataGrid.Columns>
        <DataGridTextColumn Header="Qwe"/>
        <DataGridTextColumn Header="Rty"/>
        <DataGridTextColumn Header="Uio"/>
    </DataGrid.Columns>
    <DataGrid.Resources>
        <Style TargetType="DataGridColumnHeader">
            <Setter Property="Width" Value="100"/>
        </Style>
    </DataGrid.Resources>
</DataGrid>

You can put the style not in resources, but in a special container. The difference is that the style in resources is applied to all nested qualifying elements, even nested inside cells (for example, another DataGrid inside a cell). The style in ColumnHeaderStyle will only be used in the headers of this DataGrid. Example:
<DataGrid.ColumnHeaderStyle>
    <Style TargetType="DataGridColumnHeader">
        <Setter Property="Width" Value="100"/>
    </Style>
</DataGrid.ColumnHeaderStyle>

If you specify an element style, then this style is not added to the current one, but replaces the previous style. In order for a style to be added to an existing style, the style must be inherited from the old one:
<DataGrid.ColumnHeaderStyle>
    <Style TargetType="DataGridColumnHeader" BasedOn="{StaticResource {x:Type DataGridColumnHeader}}">
        <Setter Property="Width" Value="100"/>
    </Style>
</DataGrid.ColumnHeaderStyle>

P
Peter, 2017-09-07
@petermzg

So there are styles (<Style>)
In the style you prescribe

< Style TargetType="Тип ваших элементов" >
 < Setter Property="Width" Value="250" />
< /Style >

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question