Wpf 如何设计数据网格的样式?

Wpf 如何设计数据网格的样式?,wpf,xaml,mvvm,wpfdatagrid,wpftoolkit,Wpf,Xaml,Mvvm,Wpfdatagrid,Wpftoolkit,在这里,我想给网格行提供白色和灰色的交替颜色。我做了很多尝试,但我不能做网格的样式。代码在这里 <Style TargetType="{x:Type wpftoolkit:DataGrid}"> <Setter Property="Margin" Value="0" /> <Setter Property="BorderBrush" Value="#A6A6A6" /> <Setter Property="BorderThickn

在这里,我想给网格行提供白色和灰色的交替颜色。我做了很多尝试,但我不能做网格的样式。代码在这里

<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>
在这里,StaticResource GridBgBrush在此文件的前面定义为`


请给出正确的解决方案。提前感谢

确保在GridBgBrush之后的XAML文件的resources部分中定义了样式,以便它可以引用,或者在应用程序中的ResourceDictionary中定义样式,使其可以从任何地方访问。如果看不到更多,我无法告诉你你的问题来自何方。这是定义你的风格的正确方法,如果你有兴趣看的话,我有几个例子可以说明这一点

如果您不知道的话,另一件需要注意的事情是DataGrid和DatePicker被引入了WPF4.0。这使得WPF工具包(至少对于DataGrid而言)变得不必要,如果您可以针对该版本。说了这些之后,我想如果你不知道你在用一个,然后又在用另一个,你的风格可能就行不通了

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

您还需要设置AlternationCount属性。

:您可以提供数据网格样式的链接吗?我确信样式在resourcedictionary部分定义为。感谢您的回复。
<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>