如何在DataGrid Mahapp中更改选定的行颜色

时间:2015-07-08 09:07:43

标签: c# wpf mahapps.metro

我试图更改所选行的颜色,但无法生成。 我试过这个:

<DataGrid Style="{StaticResource AzureDataGrid}">

但我能看到的唯一变化是第一个用天蓝色着色的柱子。 当我选择一行时,它变为白色,我无法看到所选行的值。我是framework主题wpf的新手,文档并不准确。有人能帮助我吗?

1 个答案:

答案 0 :(得分:3)

现在可以使用最新的Alpha版MahApps.Metro(1.1.3.x或更高版本1.2.0)更改所选的行刷

以下是主演示

的示例
<DataGrid x:Name="MetroDataGrid"
            Grid.Column="1"
            Grid.Row="1"
            RenderOptions.ClearTypeHint="Enabled"
            TextOptions.TextFormattingMode="Display"
            HeadersVisibility="All"
            Margin="5"
            SelectionUnit="FullRow"
            ItemsSource="{Binding Path=Albums}"
            AutoGenerateColumns="False">
    <DataGrid.Columns>
        <DataGridCheckBoxColumn ElementStyle="{DynamicResource MetroDataGridCheckBox}"
                                EditingElementStyle="{DynamicResource MetroDataGridCheckBox}"
                                Header="IsSelected"
                                Binding="{Binding RelativeSource={RelativeSource AncestorType=DataGridRow}, Path=IsSelected, Mode=OneWay}" />
        <DataGridTextColumn Header="Title"
                            Binding="{Binding Title}" />
        <DataGridTextColumn Header="Artist"
                            Binding="{Binding Artist.Name}" />
        <DataGridTextColumn Header="Genre"
                            Binding="{Binding Genre.Name}" />
        <controls:DataGridNumericUpDownColumn Header="Price"
                                                Binding="{Binding Price}"
                                                StringFormat="C"
                                                Minimum="0" />
    </DataGrid.Columns>
    <DataGrid.Style>
        <Style BasedOn="{StaticResource MetroDataGrid}"
                TargetType="{x:Type DataGrid}">
            <Setter Property="AlternatingRowBackground"
                    Value="{DynamicResource GrayBrush10}" />
        </Style>
    </DataGrid.Style>
    <DataGrid.RowStyle>
        <Style BasedOn="{StaticResource MetroDataGridRow}"
                TargetType="{x:Type DataGridRow}">
            <Style.Triggers>
                <DataTrigger Binding="{Binding Price, Mode=OneWay, Converter={StaticResource AlbumPriceIsTooMuchConverter}}"
                                Value="True">
                    <Setter Property="Background"
                            Value="#FF8B8B" />
                    <Setter Property="Foreground"
                            Value="Red" />
                </DataTrigger>
                <!-- IsMouseOver -->
                <MultiDataTrigger>
                    <MultiDataTrigger.Conditions>
                        <Condition Binding="{Binding Price, Mode=OneWay, Converter={StaticResource AlbumPriceIsTooMuchConverter}}"
                                    Value="True" />
                        <Condition Binding="{Binding Path=IsMouseOver, RelativeSource={RelativeSource Self}}"
                                    Value="true" />
                    </MultiDataTrigger.Conditions>
                    <Setter Property="Background"
                            Value="#FFBDBD" />
                </MultiDataTrigger>
            </Style.Triggers>
        </Style>
    </DataGrid.RowStyle>
    <DataGrid.RowValidationRules>
        <ValueConverter:AlbumPriceIsReallyTooMuchValidation ValidatesOnTargetUpdated="True"
                                                            ValidationStep="CommittedValue" />
        <ValueConverter:AlbumPriceIsReallyTooMuchValidation ValidatesOnTargetUpdated="True"
                                                            ValidationStep="UpdatedValue" />
    </DataGrid.RowValidationRules>
</DataGrid>

enter image description here