0

here i want to give alternate color white and grey to grid row . i hv done many try but i can not do styling of grid .the code is here

<Style TargetType="{x:Type wpftoolkit:DataGrid}">
    <Setter Property="Margin" Value="0" />
    <Setter Property="BorderBrush" Value="#A6A6A6" />
    <Setter Property="BorderThickness" Value="0,1,0,0"/>
    <Setter Property="Background" Value="{StaticResource GridBgBrush}" />
    <Setter Property="RowBackground" Value="White" />
    <Setter Property="AlternatingRowBackground" Value="#FFF3F6FA" />
    <Setter Property="GridLinesVisibility" Value="Horizontal" />
    <Setter Property="HorizontalGridLinesBrush" Value="Transparent" />
    <Setter Property="RowHeaderWidth" Value="0" />
</Style>

here StaticResource GridBgBrush define earlier on this file as`

plz give proper solution .thanks in advance.

chriga
  • 798
  • 2
  • 12
  • 29
Pratik
  • 167
  • 3
  • 11

2 Answers2

0

Make sure that your style is either defined within the resources section of your XAML file (after your GridBgBrush, so that it can reference it), or in a ResourceDictionary in your App somewhere making it accessible from anywhere. Without seeing more, I can't tell you where your problem is coming from. That is the correct way to define your style and I have several examples of this working as expected if you're interested in seeing them.

Another thing to note in case you didn't know, is that DataGrid (along with DatePicker) was introduced into WPF v4.0. This makes the WPF Toolkit (at least for the purposes of the DataGrid) unnecessary if you can target that version. After saying that, I suppose there's the slight chance that if you weren't aware you were using one and then styling the other, your style wouldn't work.

<XmlDataProvider x:Key="myData" Source="Data.xml" IsAsynchronous="True" />
<Style TargetType="{x:Type DataGrid}" x:Key="myStyle">
    <Setter Property="AlternatingRowBackground" Value="Red"/>
</Style>

<Grid>
<DataGrid ItemsSource="{Binding Source={StaticResource myData}, XPath=persons/person}" AutoGenerateColumns="False" Style="{StaticResource myStyle}">
        <DataGrid.Columns>
            <DataGridTemplateColumn>
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding XPath=firstname}" />
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
            <DataGridTemplateColumn>
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding XPath=lastname}" />
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
        </DataGrid.Columns>
    </DataGrid>
</Grid>
phixed
  • 478
  • 2
  • 7
  • :can u give link for data grid styling?i am sure style is defined in the resourcedictionary section as .thank for replying. – Pratik Apr 26 '12 at 01:48
0

You need to set AlternationCount property too.

Dtex
  • 2,593
  • 1
  • 16
  • 17