简体   繁体   English

如何在编写 WPF 单元格样式时停止工具提示

[英]How to stop tooltips over writing WPF Cell Style

I am currently working on a an App with a colleague that displays quarterly account figures in a data grid.我目前正在与一位同事一起开发一个应用程序,该应用程序在数据网格中显示季度账户数据。 There are some checks that are done on certain columns to see if there are over/under certain thresholds.对某些列进行了一些检查,以查看是否超过/低于某些阈值。

My colleague built the horizontal Data Grid view/layout (columns are rotated and displays cells left to right)and my task is to format the text and add tool tips to the cells where checks were performed and failed.我的同事构建了水平数据网格视图/布局(列旋转并从左到右显示单元格),我的任务是格式化文本并将工具提示添加到执行检查和失败的单元格。 Thus highlighting on the cell there is issue with the value.因此在单元格上突出显示值存在问题。

The default cells style is:默认的单元格样式是:

<Style TargetType="{x:Type DataGridCell}">
            <Setter Property="Focusable" Value="False" />
            <Setter Property="IsTabStop" Value="False" />
            <Setter Property="LayoutTransform">
                <Setter.Value>
                    <TransformGroup>
                        <RotateTransform Angle="-90"/>
                        <ScaleTransform ScaleX="1" ScaleY="-1" />
                    </TransformGroup>
                </Setter.Value>
            </Setter>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type DataGridCell}">
                        <Grid Background="{DynamicResource AppGlobalBackground}">
                            <ContentPresenter VerticalAlignment="Center"
                                              HorizontalAlignment="Left"/>
                            <TextBlock>
                                <ContentPresenter Margin="0,2,10,2"/>
                            </TextBlock>
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>

            <Style.Triggers>
                <Trigger Property="IsSelected" Value="True">
                    <Setter Property="Background" Value="{DynamicResource AppGlobalBackground}" />
                    <Setter Property="Foreground" Value="{DynamicResource AppGlobalForeground}" />
                    <Setter Property="BorderBrush" Value="{DynamicResource AppGlobalBackground}" />
                </Trigger>

                <Trigger Property="IsFocused" Value="True">
                    <Setter Property="IsSelected" Value="True" />
                </Trigger>
            </Style.Triggers>
        </Style>

The Data Grid behaves as expected without the tooltip在没有工具提示的情况下,数据网格的行为符合预期

 <DataGridTextColumn Binding="{Binding Tax}" >
   <DataGridTextColumn.HeaderTemplate >
            <DataTemplate>
                  <TextBlock Text="{Binding DataContext.ViewLabels.Tax,  RelativeSource= 
                                    RelativeSource AncestorType=DataGrid}}" />
           </DataTemplate>
    </DataGridTextColumn.HeaderTemplate>

However, once a tool tip is added it overrides the Cell style and rotates the data in the cell但是,一旦添加了工具提示,它就会覆盖单元格样式并旋转单元格中的数据

                        <DataGridTextColumn Binding="{Binding Tax}" >
                        <DataGridTextColumn.HeaderTemplate >
                            <DataTemplate>
                                <TextBlock Text="{Binding DataContext.ViewLabels.Tax,  RelativeSource={RelativeSource AncestorType=DataGrid}}" />
                            </DataTemplate>
                        </DataGridTextColumn.HeaderTemplate>

                        <DataGridTextColumn.CellStyle>
                            <Style TargetType="DataGridCell" >
                                <Setter Property="ToolTip" >
                                    <Setter.Value>
                                        <ToolTip Visibility="{Binding Path=TaxTT, Converter={StaticResource StringToVisibleTT}}" >
                                            <TextBlock Text="{Binding TaxTT}" />
                                        </ToolTip>
                                    </Setter.Value>
                                </Setter>
                                <Setter Property="Foreground" Value="{Binding Path=TaxTT,  Converter={StaticResource FormattingConverterTT}}"/>
                            </Style>
                        </DataGridTextColumn.CellStyle>
                    </DataGridTextColumn>

note The visibility converter checks if the tool tip (TaxTT) is an empty string or not & the format converter turns the text red (if the tool tip has text).注意可见性转换器检查工具提示 (TaxTT) 是否为空字符串,格式转换器将文本变为红色(如果工具提示有文本)。

I have read quite a few stack overflow articles about visual trees, but haven't been able to find one that stops it from overriding the normal cells.我已经阅读了很多关于可视化树的堆栈溢出文章,但还没有找到一篇可以阻止它覆盖正常单元格的文章。 Is it at all possible?有可能吗?

custom CellStyle should inherit base style for DataGridCell:自定义 CellStyle 应继承 DataGridCell 的基本样式:

<DataGridTextColumn.CellStyle>
    <Style TargetType="DataGridCell" BasedOn="{StaticResource {x:Type DataGridCell}}">
        <Setter Property="ToolTip" >
            <Setter.Value>
                <ToolTip Visibility="{Binding Path=TaxTT, Converter={StaticResource StringToVisibleTT}}" >
                    <TextBlock Text="{Binding TaxTT}" />
                </ToolTip>
            </Setter.Value>
        </Setter>
        <Setter Property="Foreground" Value="{Binding Path=TaxTT,  Converter={StaticResource FormattingConverterTT}}"/>
    </Style>
</DataGridTextColumn.CellStyle>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何阻止ToolTips继承我的TextBlock样式? - How can I stop ToolTips from inheriting my TextBlock style? 如何在WPF中基于条件运算符更改数据网格单元格的样式 - How to change style of data grid cell based on conditional operator in WPF WPF DataGridTextColumn如何根据单元格的内容将前景色绑定为样式 - WPF DataGridTextColumn how to bind the Foreground color in a style based on the content of the cell 如何使工具提示具有圆角-WPF - How to make ToolTips have rounded corners - WPF C# WPF - 如何以编程方式在数据网格单元格上移动组合框 - C# WPF - how to move a comboxbox over a datagrid cell programmatically 具有单元格样式的WPF DataGrid —同一列中的不同单元格样式 - WPF DataGrid with cell style — different cell style in same column 为WPF ProgressBar编写“进度派”样式 - Writing a “Progress Pie” style for WPF ProgressBar "在 WPF 列表框中,如何将项目的焦点选择样式“复制”到非焦点选择样式?" - In a WPF ListBox, how to "copy" the focused selection style for items over to the non-focused selection style? 如何在WPF DataGrid单元格中获取鼠标悬停单元格的行和列? - How to get row and column of a moused-over cell in WPF DataGrid cell? 使用转换器设置WPF单元格样式 - Set WPF Cell Style Using A Converter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM