2013-03-26 8 views
16

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

  1. Change WPF Datagrid Row Color

  2. How do I programatically change datagrid row color in WPF?

  3. Programmatically assigning a color to a row in DataGrid

  4. Change DataGrid cell colour based on values

एट अल।

मैंने CellStyleMSDN DataGrid pages पर संपत्ति देखी है लेकिन इसके उपयोग के बावजूद इसका उपयोग मेरे लिए बिल्कुल स्पष्ट नहीं है।

रनटाइम पर पूरे कॉलम के पृष्ठभूमि रंग को कैसे बदला जाए?

आपके समय के लिए धन्यवाद।

उत्तर

20

मुझे इसे काम करने का एकमात्र तरीका है कॉलम सेट करके, (ऑटोगनेरेट का उपयोग न करके)।

<Window x:Class="WpfApplication1.MainWindow" ...> 
<Window.Resources> 
    <SolidColorBrush x:Key="clBr" Color="White" /> 
</Window.Resources> 
... 

कॉलम: आप प्रत्येक स्तंभ CellStyle सेट और एक स्थिर संसाधन के लिए पृष्ठभूमि है कि आप Window.Resources में घोषणा कर सकते हैं बाध्य करने के लिए की जरूरत है

<DataGrid x:Name="Frid" ItemsSource="{Binding Path=.}"> 
     <DataGrid.Columns> 
      <DataGridTextColumn Header="First Name" 
           Binding="{Binding Path=FirstName}"> 

      </DataGridTextColumn> 

      <DataGridTextColumn Header="Last Name" 
           Binding="{Binding Path=LastName}"> 

      </DataGridTextColumn> 
     </DataGrid.Columns> 
    </DataGrid> 

तब: ऐसा करने के लिए पहली बात यह कॉलम को परिभाषित है :

   <DataGridTextColumn Header="First Name" 
            Binding="{Binding Path=FirstName}"> 
       <DataGridTextColumn.CellStyle> 
        <Style TargetType="DataGridCell"> 
         <Setter Property="Background" 
           Value="{StaticResource clBr}" /> 
        </Style> 
       </DataGridTextColumn.CellStyle> 
      </DataGridTextColumn> 

तो आप या तो कोड या xaml मैनिपुलेशन द्वारा स्थैतिक संसाधन में हेरफेर कर सकते हैं।

उम्मीद है कि यह मदद करता है।

+0

आपके समय के लिए धन्यवाद लेकिन मैं जानना चाहता हूं कि रन-टाइम पर यह कैसे करना है क्योंकि मेरे पास कॉलम चरम हैं और रन-टाइम पर बनाए गए हैं। सभी बेहतरीन ... – MoonKnight

+0

मैं इसे रन टाइम पर करना चाहता हूं। मैं खिड़की के भार पर डेटाटेड के साथ बाध्यकारी हूँ। तो यह कैसे किया जा सकता है? –

+0

मैंने आपके उत्तर में जो संकेत दिया है, वह काम करता है। लेकिन रनटाइम के दौरान मैं इसे प्रोग्रामिक रूप से कैसे बदल सकता हूं? – Kokombads

11

एक वर्ष काटा, लेकिन यहाँ है कि कैसे आप (autogen स्तंभों के लिए) प्रोग्राम के रूप में यह कर सकते हैं है:

private void dgvMailingList_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e) 
{ 
    e.Column.CellStyle = new Style(typeof(DataGridCell)); 
    e.Column.CellStyle.Setters.Add(new Setter(DataGridCell.BackgroundProperty, new SolidColorBrush(Colors.LightBlue))); 
} 

एक ही दृष्टिकोण भी गैर autogen कॉलम के लिए लागू किया जा सकता है।