2011-08-17 23 views
5

正如我所看到的,很多人遇到了這個確切的問題,但我不明白爲什麼我的情況不起作用,它開始讓我瘋狂。在ContentPresenter中自動生成文本塊的樣式

上下文:我有一個DataGrid這將根據每個單元格的值進行着色。因此,我有一個動態樣式來解析每個單元格使用的實際模板。背景現在相應地工作。

新問題:當我有深色背景時,我希望字體顏色爲白色,字體粗細爲粗體,以便文本正確可讀。而且......我無法正確地設計它。

我讀到一些帖子#1:

This one fits my problem but doesn't provide me any working solution This one is also clear and detail but... duh This is almost the same problem as me but... Solution does not work

這裏是我試過到目前爲止:

<!-- Green template--> 
    <ControlTemplate x:Key="Green" TargetType="{x:Type tk:DataGridCell}"> 
     <Grid Background="Green"> 
      <ContentPresenter 
          HorizontalAlignment="Center" 
             VerticalAlignment="Center"> 
       <ContentPresenter.Resources> 
        <Style BasedOn="{StaticResource BoldCellStyle}" TargetType="{x:Type TextBlock}" /> 
       </ContentPresenter.Resources> 
      </ContentPresenter> 
     </Grid> 
    </ControlTemplate> 

不工作。背景是綠色的,但文字保持黑色&不粗體。

順便說一句,在BoldCellStyle是那麼容易,因爲它可以:

<Style x:Key="BoldCellStyle" TargetType="{x:Type TextBlock}"> 
    <Setter Property="FontWeight" Value="Bold"/> 
    <Setter Property="Foreground" Value="White" /> 
</Style> 

好。第二次嘗試(這是一個真正的愚蠢的一個,但...)

<!-- Green template --> 
    <ControlTemplate x:Key="Green" TargetType="{x:Type tk:DataGridCell}"> 
     <Grid Background="Green"> 
      <ContentPresenter 
          HorizontalAlignment="Center" 
             VerticalAlignment="Center"> 
       <ContentPresenter.Resources> 
        <Style x:Key="BoldCellStyle" TargetType="{x:Type TextBlock}"> 
         <Setter Property="FontWeight" Value="Bold"/> 
         <Setter Property="Foreground" Value="White" /> 
        </Style> 

       </ContentPresenter.Resources> 
      </ContentPresenter> 
     </Grid> 
    </ControlTemplate> 

也不管用。

然後,我試圖與ContentPresenter的性能發揮:

<!-- Green template --> 
<ControlTemplate x:Key="Green" TargetType="{x:Type tk:DataGridCell}"> 
    <Grid Background="Green"> 
     <ContentPresenter TextElement.FontWeight="Bold" TextElement.Foreground="White" TextBlock.Foreground="White" 
         HorizontalAlignment="Center" 
            VerticalAlignment="Center" /> 
    </Grid> 
</ControlTemplate> 

...如果您可以期待,這甚至不工作。

好奇,我用Snoop瀏覽我的界面的所有組件。 在前兩種情況下,探聽居然讓我發現,每個單元是Grid用含有TextBlock一個ContentPresenter和實際Style ...但是... TextBlock的屬性並不適用,FontWeight仍正常。

最後一種情況下,更令人吃驚的,我可以看到,探聽讓我發現,我們其實有正確的性質(即TextElement.FontWeight="Bold")一個ContentPresenter,但自動生成TextBlock下 - 還 - 沒有風格。

我不明白我在這裏想念什麼。我嘗試了幾乎所有我可能在這裏做的事情,而TextBlock仍然是非格式化的。

這裏的任何想法?再次感謝!

回答

2

的從DataGridBoundColumn(除了DataGridTemplateColumn)導出DataGridColumns具有在創建時被施加到所述一個TextBlock屬性ElementStyle。對於例如DataGridTextColumn它看起來像這樣

static DataGridTextColumn() 
{ 
    ElementStyleProperty.OverrideMetadata(typeof(DataGridTextColumn), 
     new FrameworkPropertyMetadata(DefaultElementStyle)); 
    // ... 
} 

它覆蓋了ElementStyle元數據,並提供了一個新的默認值,DefaultElementStyle,這基本上只是設定了TextBlock默認的保證金。

public static Style DefaultElementStyle 
{ 
    get 
    { 
     if (_defaultElementStyle == null) 
     { 
      Style style = new Style(typeof(TextBlock)); 
      // Use the same margin used on the TextBox to provide space for the caret 
      style.Setters.Add(new Setter(TextBlock.MarginProperty, new Thickness(2.0, 0.0, 2.0, 0.0))); 
      style.Seal(); 
      _defaultElementStyle = style; 
     } 
     return _defaultElementStyle; 
    } 
} 

這種風格在代碼每次一個新DataGridCellelement.Style = style;創建,這是壓倒你想設置的風格,即使你試圖隱式設置其設置。

據我所知,你必須重複此爲您列

<DataGridTextColumn Header="Column 1" ElementStyle="{StaticResource BoldCellStyle}" .../> 
<DataGridTextColumn Header="Column 2" ElementStyle="{StaticResource BoldCellStyle}" .../> 
+0

哇。我絕對不得不這樣做?我在這裏工作的主要目的是分別對每個單元格進行樣式設置(其中一些是粗體,另外一些是正常的) – Damascus 2011-08-17 13:26:31