2011-08-17 13 views
5

में एक टेक्स्टब्लॉक स्टाइलिंग स्टाइलिंग जैसा कि मैंने देखा, बहुत से लोग इस सटीक समस्या में भाग गए लेकिन मुझे समझ में नहीं आया कि मेरा मामला क्यों काम नहीं कर रहा है और यह मुझे पागल करने शुरू कर रहा है।एक सामग्री प्रदाता

संदर्भ: मेरे पास DataGrid है जो प्रत्येक सेल के मानों के अनुसार रंगीन होना है। इसलिए, मेरे पास प्रत्येक सेल के लिए उपयोग किए जाने वाले वास्तविक टेम्पलेट को हल करने वाली गतिशील शैली है। पृष्ठभूमि अब तदनुसार काम करते हैं।

नई समस्या: जब मेरे पास एक गहरा पृष्ठभूमि है, तो मैं फ़ॉन्ट रंग सफेद होना चाहता हूं और फ़ॉन्ट वजन बोल्ड होना चाहिए ताकि पाठ सही ढंग से पठनीय हो। और ... मैं इसे सही ढंग से शैली नहीं बना सकता।

मुझे इस बारे में कुछ Stackoverflow पोस्ट पढ़ने:

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

काम नहीं करता:

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

यहाँ मैं अब तक की कोशिश की है। पृष्ठभूमि हरा है, लेकिन टेक्स्ट बोल्ड & में बोल्ड नहीं रहता है।

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

और ... आप उम्मीद कर सकते हैं के रूप में, यह भी काम नहीं करता।

जानबूझकर, मैंने अपने इंटरफ़ेस के सभी घटकों को ब्राउज़ करने के लिए स्नूप का उपयोग किया। पहले दो मामलों में, स्नूप वास्तव में मुझे दिखाता है कि प्रत्येक सेल Grid है ContentPresenter जिसमें TextBlock और वास्तविक Style है लेकिन ... TextBlock की गुण लागू नहीं होती हैं और FontWeight अभी भी सामान्य है।

अंतिम मामला है, और भी अधिक चौंकाने वाला है, मैं देख सकता हूँ कि स्नूप मुझे पता चलता है कि हम वास्तव में सही गुण (यानी TextElement.FontWeight="Bold") के साथ एक ContentPresenter है, लेकिन ऑटोजनरेटेड TextBlock किया जा रहा है - अभी भी - स्टाइल नहीं।

मुझे यह नहीं मिल रहा है कि मैं यहां क्या खो रहा हूं। मैंने कोशिश की क्योंकि आप लगभग सभी संभवतः यहां देख सकते हैं, और TextBlock एस गैर प्रारूपित नहीं रह रहे हैं।

कोई विचार यहाँ है? एक बार फिर धन्यवाद!

उत्तर

2

DataGridColumns कि DataGridBoundColumn (सभी DataGridTemplateColumn को छोड़कर) से निकाले जाते हैं एक संपत्ति ElementStyle कि TextBlock को लागू किया जाता है, जब यह बनाई गई है है। उदाहरण के लिए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

+0

@ डेमस्कस: हाँ, जहां तक ​​मुझे पता है। आप अभी भी एक ही स्टाइल का उपयोग कर सकते हैं, आपको बस इसे प्रत्येक कॉलम पर लागू करना होगा। –

+0

मैंने अभी कोशिश की है और यह "काम करता है" (हाँ, यह मेरे सभी कोशिकाओं को बोल्ड करने के लिए सेट करता है), मैं इसे बनाने के लिए ट्रिगर्स के साथ खेलने की कोशिश करूंगा:/ – Damascus

संबंधित मुद्दे