【问题标题】:How to stop tooltips over writing WPF Cell Style如何在编写 WPF 单元格样式时停止工具提示
【发布时间】:2022-12-05 20:21:18
【问题描述】:

我目前正在与一位同事一起开发一个应用程序,该应用程序在数据网格中显示季度账户数据。对某些列进行了一些检查,以查看是否超过/低于某些阈值。

我的同事构建了水平数据网格视图/布局(列旋转并从左到右显示单元格),我的任务是格式化文本并将工具提示添加到执行检查和失败的单元格。因此在单元格上突出显示值存在问题。

默认的单元格样式是:

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

在没有工具提示的情况下,数据网格的行为与预期一致

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

但是,一旦添加了工具提示,它就会覆盖单元格样式并旋转单元格中的数据

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

笔记可见性转换器检查工具提示 (TaxTT) 是否为空字符串,格式转换器将文本变为红色(如果工具提示有文本)。

我已经阅读了很多关于可视化树的堆栈溢出文章,但一直未能找到阻止它覆盖正常单元格的文章。有可能吗?

【问题讨论】:

    标签: wpf datagrid wpf-controls


    【解决方案1】:

    自定义 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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-05-10
      • 2017-01-15
      • 1970-01-01
      • 2019-06-10
      • 1970-01-01
      • 2011-05-24
      • 1970-01-01
      相关资源
      最近更新 更多