0

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

<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 在此文件的前面定义为`

请给出适当的解决方案。提前谢谢。

4

2 回答 2

0

您还需要设置 AlternationCount 属性。

于 2012-04-26T10:01:01.493 回答
0

确保您的样式是在 XAML 文件的资源部分中定义的(在您的 GridBgBrush 之后,以便它可以引用它),或者在您的应用程序中的某个 ResourceDictionary 中定义,使其可以从任何地方访问。没有看到更多,我不能告诉你你的问题来自哪里。这是定义你的风格的正确方法,如果你有兴趣看到它们,我有几个按预期工作的例子。

如果您不知道,另一件需要注意的事情是 DataGrid(连同 DatePicker)被引入 WPF v4.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>

<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>
于 2012-04-26T01:40:24.780 回答