[英]How to stop tooltips over writing WPF Cell Style
我目前正在與一位同事一起開發一個應用程序,該應用程序在數據網格中顯示季度賬戶數據。 對某些列進行了一些檢查,以查看是否超過/低於某些閾值。
我的同事構建了水平數據網格視圖/布局(列旋轉並從左到右顯示單元格),我的任務是格式化文本並將工具提示添加到執行檢查和失敗的單元格。 因此在單元格上突出顯示值存在問題。
默認的單元格樣式是:
<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) 是否為空字符串,格式轉換器將文本變為紅色(如果工具提示有文本)。
我已經閱讀了很多關於可視化樹的堆棧溢出文章,但還沒有找到一篇可以阻止它覆蓋正常單元格的文章。 有可能嗎?
自定義 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.