WPF DataGridCell样式 - 在多列

时间:2016-03-03 13:05:44

标签: wpf vb.net binding tooltip

我有一个具有未知列数的DataGrid。从屏幕截图中您可以看到有3个EF列:

enter image description here

您看不到的是,每个EF列都有其他隐藏的列,其中包含有关其各自EF列的其他信息。例如,在EF1之前有一个隐藏列,名为1Status(用于根据其值有条件地格式化单元格背景),另一个隐藏列名为1AssMeth,它保存文本以显示在工具提示中。 Ergo - 对于每个 EF [X] 列,各自有 XStatus &的 XAssMeth

如您所见,我有条件背景颜色。

使用ValueConverter处理背景颜色,并使用以下资源实现:

<Window.Resources>
    <ResourceDictionary>

        <local:PercVerifiedConverter x:Key="PercVerifiedConverter" />
        <Style x:Key="PercVerified" TargetType="{x:Type DataGridCell}" >
            <Style.Triggers>
                <DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Converter={StaticResource PercVerifiedConverter}}" Value="Red">
                    <Setter Property="Background">
                        <Setter.Value>
                            <LinearGradientBrush>
                                <GradientStop Color="DarkRed" Offset="0"/>
                                <GradientStop Color="White" Offset="1"/>
                            </LinearGradientBrush>
                        </Setter.Value>
                    </Setter>
                    <Setter Property="Foreground" Value="White"></Setter>
                    <Setter Property="Margin" Value="0"></Setter>
                </DataTrigger>
                <DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Converter={StaticResource PercVerifiedConverter}}" Value="Green">
                    <Setter Property="Background">
                        <Setter.Value>
                            <LinearGradientBrush>
                                <GradientStop Color="DarkGreen" Offset="0"/>
                                <GradientStop Color="White" Offset="1"/>
                            </LinearGradientBrush>
                        </Setter.Value>
                    </Setter>
                    <Setter Property="Foreground" Value="White"></Setter>
                    <Setter Property="Margin" Value="0"></Setter>
                </DataTrigger>
                <DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Converter={StaticResource PercVerifiedConverter}}" Value="Orange">
                    <Setter Property="Background">
                        <Setter.Value>
                            <LinearGradientBrush>
                                <GradientStop Color="DarkOrange" Offset="0"/>
                                <GradientStop Color="White" Offset="1"/>
                            </LinearGradientBrush>
                        </Setter.Value>
                    </Setter>
                    <Setter Property="Foreground" Value="White"></Setter>
                    <Setter Property="Margin" Value="0"></Setter>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </ResourceDictionary>
</Window.Resources>

所以这一切都适用于背景颜色......但是我还希望在样式中添加一个工具提示以返回相应XAssMeth列中的值。所以,为了让事情滚动,我做了以上风格...

        <local:PercVerifiedConverter x:Key="PercVerifiedConverter" />
        <Style x:Key="PercVerified" TargetType="{x:Type DataGridCell}" >

            <Setter Property="ToolTip" Value="{Binding 1AssMeth}" />

            <Style.Triggers> .....

您可能会想到按预期返回工具提示,但每列都有相同的工具提示 - 1AssMeth 列中的值。

将鼠标悬停在EF 1的第一行,系统会显示此工具提示

然后将鼠标悬停在任何后续列的第一行上,并显示相同的工具提示文本。

我希望这种行为对此感到高兴。我需要知道的是我如何应用额外的绑定到EF [X]列,以便从相应的XAssMeth列中获取正确的工具提示文本 - 而不会失去我的背景颜色样式的功能。

如果可能有帮助,我可以提供有关转换器或其他任何信息的更多信息 - 但我希望它很简单?即我需要另一个转换器来获得正确的相应列标题XAssMeth或类似的东西吗?如果是这样,那么一个例子就是惊人的!

P.S。我将寻求扩展工具提示以制作定制的东西 - 所以如果有人能够把它扔进混音中,那就太棒了!

1 个答案:

答案 0 :(得分:1)

要使用转换器执行此操作,例如DataTriggers正在使用:

<local:PercVerifiedConverter x:Key="PercVerifiedConverter" />
<local:TooltipConverter x:Key="TooltipConverter" />
<Style x:Key="PercVerified" TargetType="{x:Type DataGridCell}" >

        <Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Self}, Converter={StaticResource TooltipConverter}}" />

    <Style.Triggers>...

转换器的VB:

Public Class TooltipConverter
    Implements IValueConverter

    Public Function Convert(value As Object, targetType As Type, parameter As Object, culture As CultureInfo) As Object Implements IValueConverter.Convert
        Convert = Nothing
        Dim cell As DataGridCell
        Dim dc As System.Data.DataRowView

        ' Get the DataGridCell passed in
        cell = TryCast(value, DataGridCell)
        If IsNothing(cell) Then Exit Function

        ' Get the cell's DataContext as our data class
        dc = TryCast(cell.DataContext, System.Data.DataRowView)
        If IsNothing(dc) Then Exit Function

        ' Get the cell's column - need it for the binding
        Dim tc As DataGridTextColumn ' Assuming your cells are DataGridTextColumns
        tc = TryCast(cell.Column, DataGridTextColumn)
        If IsNothing(tc) Then Exit Function

        ' Get the column's binding
        Dim b As Binding
        b = TryCast(tc.Binding, System.Windows.Data.Binding)
        If IsNothing(b) Then Exit Function

        ' Get the path off the binding
        Dim path As String
        path = b.Path.Path ' Name of the property this column is bound to - 1AssMeth, EF1, etc...

        ' If one of the "EF" properties, convert path to the appropriate "AssMeth" path
        If path.Contains("EF") Then
            Dim pvNum = path.Replace("EF", String.Empty) ' EF1 becomes 1
            path = pvNum + "AssMeth" ' path is now 1AssMeth
        End If
        If path.Contains("AssMeth") Then
            Convert = dc(path)
        End If
    End Function

    Public Function ConvertBack(value As Object, targetType As Type, parameter As Object, culture As CultureInfo) As Object Implements IValueConverter.ConvertBack
        Throw New NotImplementedException()
    End Function
End Class

编辑: 要自定义工具提示,请将您的样式更新为以下内容:

<Style x:Key="PercVerified" TargetType="{x:Type DataGridCell}" >
    <Style.Resources>
        <Style x:Key="{x:Type ToolTip}" TargetType="{x:Type ToolTip}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate>
                        <Border Background="Beige" BorderBrush="Navy" BorderThickness="2">
                            <StackPanel Width="400" Height="200">
                                <TextBlock Background="Bisque" Foreground="Maroon" Text="ToolTip Header" Margin="10" HorizontalAlignment="Stretch" />
                                <TextBlock Text="My tooltip text is:" />
                                <TextBlock Text="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Content}" Background="DarkBlue" Foreground="White" Margin="25" />
                                <TextBlock Text="Something else could go here." />
                            </StackPanel>
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Style.Resources>
    <Style.Setters>
        <Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Self}, Converter={StaticResource TooltipConverter}}" />
    </Style.Setters>
    <Style.Triggers>...