【问题标题】:How to do styling of data grid?如何做数据网格的样式?
【发布时间】:2012-05-06 17:51:21
【问题描述】:

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

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

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

【问题讨论】:

    标签: wpf xaml mvvm wpfdatagrid wpftoolkit


    【解决方案1】:

    确保您的样式在 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>
    

    【讨论】:

    • :你能提供数据网格样式的链接吗?我确定样式在资源字典部分中定义为 。感谢回复。
    【解决方案2】:

    您还需要设置 AlternationCount 属性。

    【讨论】:

      猜你喜欢
      • 2016-11-10
      • 1970-01-01
      • 2012-03-07
      • 2010-10-07
      • 2014-06-11
      • 1970-01-01
      相关资源
      最近更新 更多