Noxxys
Noxxys

Reputation: 919

DataGridTextColumn style with wrap and tooltip

In my C# / WPF application, I have a Datagrid with several DataGridTextColumn column using both text wrapping and a tooltip.

I could write each column like this (and this works fine):

<DataGridTextColumn Header="Name" Binding="{Binding Name}">
    <DataGridTextColumn.ElementStyle>
        <Style>
            <Setter Property="TextBlock.TextWrapping" Value="Wrap" />
        </Style>
    </DataGridTextColumn.ElementStyle>
    <DataGridTextColumn.CellStyle>
        <Style TargetType="DataGridCell">
            <Setter Property="ToolTip" Value="Some tooltip text" />
        </Style>
    </DataGridTextColumn.CellStyle>
</DataGridTextColumn>

But I would like to define a common style that can set both the wrapping and the tooltip text, knowing that the tooltip text will be different for each column. The purpose is to avoid code redundancy and make it clearer.

So far, here's my style:

<Window.Resources>
    <Style x:Key="WrapStyle" TargetType="{x:Type DataGridCell}">
        <Style.Setters>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type DataGridCell}">
                        <TextBox Text="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Content.Text}" TextWrapping="Wrap">
                            <TextBox.ToolTip>
                                <ToolTip>
                                    <ToolTip.Content>
                                        <TextBlock Text="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Tooltip}" />
                                    </ToolTip.Content>
                                </ToolTip>
                            </TextBox.ToolTip>
                        </TextBox>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style.Setters>
    </Style>
</Window.Resources>

And my column:

<DataGridTextColumn Header="Name" Binding="{Binding Name}" CellStyle="{StaticResource WrapStyle}" />

The problem is that I can't specify a tooltip to pass to the style. Is there a way to do it without writing 5 lines of DataGridTextColumn.CellStyle for each column? Thanks

Upvotes: 1

Views: 1223

Answers (1)

piyush
piyush

Reputation: 29

Modify the style to -

<Window.Resources>
    <Style x:Key="WrapStyle" TargetType="DataGridCell">
        <Style.Setters>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type DataGridCell}">
                        <TextBox Text="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Content.Text}" TextWrapping="Wrap">
                            <TextBox.ToolTip>
                                <ToolTip>
                                    <ToolTip.Content>
                                        <TextBlock Text="{Binding Path=Tooltip}"></TextBlock>
                                    </ToolTip.Content>
                                </ToolTip>
                            </TextBox.ToolTip>
                        </TextBox>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style.Setters>
    </Style>
</Window.Resources>

Upvotes: 1

Related Questions