C# 带有换行和工具提示的DataGridTextColumn样式

C# 带有换行和工具提示的DataGridTextColumn样式,c#,wpf,wpfdatagrid,wpf-style,C#,Wpf,Wpfdatagrid,Wpf Style,在我的C#/WPF应用程序中,我有一个Datagrid,其中有几个DataGridTextColumn列,同时使用文本换行和工具提示 我可以这样写每一篇专栏文章(这很好): 但是我想定义一种通用样式,可以设置换行和工具提示文本,因为知道每列的工具提示文本都是不同的。其目的是避免代码冗余并使其更清晰 到目前为止,我的风格如下: <Window.Resources> <Style x:Key="WrapStyle" TargetType="{x:Type DataGri

在我的C#/WPF应用程序中,我有一个
Datagrid
,其中有几个
DataGridTextColumn
列,同时使用文本换行和工具提示

我可以这样写每一篇专栏文章(这很好):


但是我想定义一种通用样式,可以设置换行和工具提示文本,因为知道每列的工具提示文本都是不同的。其目的是避免代码冗余并使其更清晰

到目前为止,我的风格如下:

<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>

我的专栏:

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

问题是我无法指定要传递给样式的工具提示。有没有一种方法可以不为每列写入5行
DataGridTextColumn.CellStyle
? 谢谢

将样式修改为-

<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>


如何将工具提示文本从DataGridTextColumn传递到样式?它不接受工具提示参数tooltip可以是当前绑定的项的属性。例如,如果Student集合绑定为items source,并且Student类具有Tooltip属性,则可以将其设置为如上所示的Tooltip。@Piyus您能否告诉我如何在DataGridTextColumn中使用上述样式?您在前面的注释中解释过,但我没有理解它。下面是它的定义,我想了解如何将工具提示文本传递给这个样式?
<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>