Search code examples
c#wpfdatagridresourcedictionaryword-wrap

Set TextWrapping to Wrap for all Cells, using template


So, I`v got the next problem: I am setting up a customized table (which is getting filled from the DB), where the content of the cells will be wraped. I am able to set the view correctly. The code is:

<Grid Style="{StaticResource FormBackground}">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="20" />
        <ColumnDefinition Width="*" />
        <ColumnDefinition Width="20" />
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="15" />
        <RowDefinition Height="*" />
        <RowDefinition Height="40" />
        <RowDefinition Height="10" />
    </Grid.RowDefinitions>

    <Grid Grid.Column="1" Grid.Row="1">
        <Border Style="{StaticResource DGBorder}">
            <DataGrid x:Name="usuariosGrid" 
                  AutoGenerateColumns="False" 
                  CanUserAddRows="False"
                  Grid.Row="0" 
                  Style="{StaticResource Table}" >
                <DataGrid.Columns>
                    <DataGridTextColumn Header="ID" Width="40" Binding="{Binding Id}" IsReadOnly="True">
                        <DataGridTextColumn.ElementStyle>
                            <Style TargetType="TextBlock">
                                <Setter Property="TextWrapping" Value="Wrap" />
                            </Style>
                        </DataGridTextColumn.ElementStyle>
                    </DataGridTextColumn>
                    <DataGridTextColumn Header="Nombre" Width="*" Binding="{Binding Nombre}" IsReadOnly="True">
                        <DataGridTextColumn.ElementStyle>
                            <Style TargetType="TextBlock">
                                <Setter Property="TextWrapping" Value="Wrap" />
                            </Style>
                        </DataGridTextColumn.ElementStyle>
                    </DataGridTextColumn>
                    <DataGridTextColumn Header="Apellido Paterno" Width="*" Binding="{Binding ApellidoPaterno}" IsReadOnly="True">
                        <DataGridTextColumn.ElementStyle>
                            <Style TargetType="TextBlock">
                                <Setter Property="TextWrapping" Value="Wrap" />
                            </Style>
                        </DataGridTextColumn.ElementStyle>
                    </DataGridTextColumn>
                    <DataGridTextColumn Header="Apellido Materno" Width="*" Binding="{Binding ApellidoMaterno}" IsReadOnly="True">
                        <DataGridTextColumn.ElementStyle>
                            <Style TargetType="TextBlock">
                                <Setter Property="TextWrapping" Value="Wrap" />
                            </Style>
                        </DataGridTextColumn.ElementStyle>
                    </DataGridTextColumn>
                    <DataGridTextColumn Header="CorreoElectronico" Width="*" Binding="{Binding CorreoElectronico}" IsReadOnly="True">
                        <DataGridTextColumn.ElementStyle>
                            <Style TargetType="TextBlock">
                                <Setter Property="TextWrapping" Value="Wrap" />
                            </Style>
                        </DataGridTextColumn.ElementStyle>
                    </DataGridTextColumn>
                </DataGrid.Columns>
            </DataGrid>
        </Border>
    </Grid>

Works good, but is pretty much ugly, cause I have to set TextBox -> TextWrapping -> Wrap custom for every column. As well - the project contains multiple DataGrids. So for sure - I want to pack it all into a ResourceDictionary, which is:

<Style x:Key="Table" TargetType="DataGrid">
    <Setter Property="Foreground" Value="{StaticResource WhiteBrush}" />
    <Setter Property="RowBackground" Value="Transparent"/>
    <Setter Property="AlternatingRowBackground" Value="{StaticResource DarkBlueFadedBrush}" />
    <Setter Property="Background" Value="Transparent"/>
    <Setter Property="BorderThickness" Value="0"/>
    <Setter Property="ColumnHeaderStyle" Value="{StaticResource DGColumnHeader}" />
    <Setter Property="RowStyle" Value="{StaticResource SelectedRow}"/>
    <Setter Property="GridLinesVisibility" Value="None" />
    <Setter Property="RowHeaderWidth" Value="0" />
    <Setter Property="BorderBrush" Value="{StaticResource LightBlueBrush}" />
</Style>

And at this point fun begins: DataGridTextColumn is not supported for setting it as style.

Well - where is a will, there is a way. Said one of the comrades around the web and gave a suggestion to use a back code. Which is fair. But I have a belief, that such type of improvements should be divided (and because it would be necessary to add back code to each one of the viewers, which contains forms).

Any suggestions, working directions, etc. would be appreciated.


Solution

    1. If you want multiple things to remain common across all DataGrids. Derive a new class from DataGrid thus creating new Control.

    2. And you can always do this : <DataGridTextColumn ElementStyle="{StaticResource tbKey}" Binding="{Binding Name}"/> .

        <Style TargetType="TextBlock" x:Key="tbKey">
             <Setter Property="TextWrapping" Value="Wrap" />
        </Style>