2011-09-09 22 views
1

我試圖將文本塊的工具提示綁定到文本塊文本綁定的值。工具提示的樣式文本塊綁定是綁定到控件模板而不是

以下工作對於應用此風格的TextBlocks:

<Style x:Key="GridCell" TargetType="{x:Type TextBlock}"> 
    <Setter Property="ToolTip" Value="{Binding Converter={StaticResource CellToolTipConverter}}"/> 
</Style> 

<DataTemplate x:Key="GridCellContentTemplate"> 
    <TextBlock Style="{StaticResource GridCell}" 
       Text="{Binding Converter=..."/> 
</DataTemplate> 

<xcdg:Column FieldName="FXRate" CellContentTemplate="{GridCellContentTemplate}" /> 

Working Tooltip

但由於一些奇怪的原因,當我嘗試過這種風格作爲一種資源來DataGrid的統計細胞,

<Style x:Key="{x:Type xcdg:StatCell}" TargetType="{x:Type xcdg:StatCell}"> 
    <Style.Resources> 
     <Style x:Key="{x:Type TextBlock}" TargetType="{x:Type TextBlock}"> 
      <Setter Property="ToolTip" Value="{Binding Converter={StaticResource CellToolTipConverter}}"/> 
     </Style> 
    </Style.Resources> 
</Style> 

<xcdg:StatCell FieldName="Limit"> 
     <TextBlock Text="{Binding Source={StaticResource Layers}, Path=StatLimit, Converter=..." /> 
</xcdg:StatCell> 

Broken Tooltip

正如您所看到的,工具提示被綁定到某個DataTemplate而不是文本框文本綁定的內容。從我可以告訴的是,這兩者沒有什麼區別,事實上後者似乎更直接。

任何人都可以找出爲什麼第二個工具提示綁定不工作的第一個方式?


我可以肯定的是,結合正在做它的方式,雖然在單元格中的文本,因爲如果我更改綁定到:

<Style x:Key="{x:Type xcdg:StatCell}" TargetType="{x:Type xcdg:StatCell}"> 
    <Style.Resources> 
     <Style x:Key="{x:Type TextBlock}" TargetType="{x:Type TextBlock}"> 
      <Setter Property="ToolTip" Value="{Binding Path=Text, RelativeSource={x:Static RelativeSource.Self}, Converter={StaticResource CellToolTipConverter}}"/> 
     </Style> 
    </Style.Resources> 
</Style> 

我得到這個:

Attempt

但是當然,我不想textblock文本屬性,我想原始值textblock是boun d到。

+0

爲什麼不簡單地使用適用於文本框的樣式?又名'' – Tejs

+0

@Tejs - 同樣的效果。將statcell更改爲「結果在工具提示中顯示」System.Windows.DataTemplate「 – Alain

+0

我剛剛注意到這一點,是您的關鍵名稱應該是一個參考?我認爲'x:Key'必須是一個簡單的字符串(例如:'x:Key =「{x:Type TextBlock}」'這對我來說看起來很奇怪,儘管它可能不算什麼(而且它只是將字符串值作爲鍵) – Tejs

回答

0

原因是文本綁定正在查看工具提示所附的對象的datacontext。恰巧,xcdg:StatCell爲了自己的目的劫持datacontext,所以任何子視覺元素都無法訪問被綁定的原始屬性。