D
D
Denis Mashanov2018-02-21 09:33:09
WPF
Denis Mashanov, 2018-02-21 09:33:09

Text Wrap how to do for all elements in a table?

Good day everyone. Styling issues arose. I can't make it so that one style is applied to all DataGridTextColumn tags, namely Wrap. Now it turned out to be done only for elements, but at the same time, Wrap does not work on the Header, and besides, you need to write an ID for access. Help me please.

<DataGrid.Resources>
                <Style TargetType="TextBlock" x:Key="txtBlok" >
                    <Setter Property="TextWrapping" Value="Wrap"/>
                </Style>
            </DataGrid.Resources>
<DataGrid.Columns>
<DataGridTextColumn Header="1" ElementStyle="{StaticResource txtBlok}"/>
<DataGridTextColumn Header="2" ElementStyle="{StaticResource txtBlok}"/>
<DataGridTextColumn Header="3" ElementStyle="{StaticResource txtBlok}"/>
</DataGrid.Columns>

It turned out to be done this way, but isn't it possible to make it under one Style and make it so that for all DataGridTextColumn?
<Style TargetType="TextBlock" x:Key="WrappedRowStyle" >
                    <Setter Property="TextWrapping" Value="Wrap"/>
                </Style>
                <Style x:Key="WrappedColumnHeaderStyle" TargetType="{x:Type DataGridColumnHeader}">
                    <Setter Property="ContentTemplate">
                        <Setter.Value>
                            <DataTemplate>
                                <TextBlock TextWrapping="Wrap" Text="{Binding}"></TextBlock>
                            </DataTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>

<DataGridTextColumn Header="1" HeaderStyle="{StaticResource WrappedColumnHeaderStyle}" ElementStyle="{StaticResource WrappedRowStyle}"/>

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question