设置DataGridCell样式触发器属性不响应

时间:2014-01-30 04:04:49

标签: c# wpf xaml datagrid styles

我有一个DataGrid,其中Foreground初始化为白色,如下所示:

<DataGridTextColumn Header="February"
                    Binding="{Binding Path=Element[February].Value}" 
                    Width="*" 
                    Foreground="White" />

但是当我尝试用鼠标悬停将前景设置为黑色时它不起作用我不知道我做错了什么我可以使用一些帮助,谢谢。

<DataGrid.CellStyle>
    <Style TargetType="DataGridCell">
        <Style.Triggers>
            <Trigger Property="IsMouseOver" Value="True">
                <Setter Property="Foreground" Value="White"/>
                <Setter Property="Background" Value="Black"/>
            </Trigger>
        </Style.Triggers>
    </Style>
</DataGrid.CellStyle>

EDIT: Full code

<Window x:Class="WPF_TEST.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="450" Width="850
        ">
    <Grid>
        <DataGrid Margin="150,50,150,50" x:Name="GridBinding" ItemsSource="{Binding Path=Elements[Month]}" Background="Transparent" RowBackground="Transparent" AutoGenerateColumns="False" AreRowDetailsFrozen="True" SelectionUnit="Cell" GridLinesVisibility="None" IsReadOnly="True">

            <DataGrid.Columns>
                <DataGridTextColumn Header="Oct." Binding="{Binding Path=Element[October].Value}" Width="*" Foreground="Black"/>
                <DataGridTextColumn Header="Nov." Binding="{Binding Path=Element[November].Value}" Width="*" Foreground="White"/>
                <DataGridTextColumn Header="Dec." Binding="{Binding Path=Element[December].Value}" Width="*" Foreground="White"/>
                <DataGridTextColumn Header="Jan." Binding="{Binding Path=Element[January].Value}" Width="*" Foreground="White"/>
                <DataGridTextColumn Header="Feb." Binding="{Binding Path=Element[February].Value}" Width="*" Foreground="Black"/>
                <DataGridTextColumn Header="Mar." Binding="{Binding Path=Element[March].Value}" Width="*" Foreground="Black"/>
                <DataGridTextColumn Header="Apr." Binding="{Binding Path=Element[April].Value}" Width="*" Foreground="Black"/>
                <DataGridTextColumn Header="May" Binding="{Binding Path=Element[May].Value}" Width="*" Foreground="Black"/>
                <DataGridTextColumn Header="June" Binding="{Binding Path=Element[June].Value}" Width="*" Foreground="Black"/>
                <DataGridTextColumn Header="July" Binding="{Binding Path=Element[July].Value}" Width="*" Foreground="Black"/>
                <DataGridTextColumn Header="Aug." Binding="{Binding Path=Element[August].Value}" Width="*" Foreground="Black"/>
                <DataGridTextColumn Header="Sep." Binding="{Binding Path=Element[September].Value}" Width="*" Foreground="Black"/>
            </DataGrid.Columns>

            <DataGrid.RowHeaderTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Path=DataContext.Element[Depth].Value, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=DataGridRow}}"/>
                </DataTemplate>
            </DataGrid.RowHeaderTemplate>

        </DataGrid>
    </Grid>

    <Window.Resources>
        <Style TargetType="{x:Type DataGridCell}">
            <Setter Property="OverridesDefaultStyle" Value="True" />
            <Setter Property="Foreground" Value="Transparent" />

            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type DataGridCell}">
                        <Border x:Name="BackgroundBorder" 
                        BorderThickness="1">

                            <ContentPresenter VerticalAlignment="Center" 
                                      Margin="4,0,6,0" />
                        </Border>

                        <ControlTemplate.Triggers>
                            <Trigger Property="IsMouseOver" Value="True">
                                <Setter Property="Foreground" Value="Black" />
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Window.Resources>
</Window>

1 个答案:

答案 0 :(得分:1)

尝试在 ControlTemplate 类型的DataGridCell中移动StyleTrigger。请使用Foreground

,而不是使用白色Transparent颜色
<Style TargetType="{x:Type DataGridCell}">
    <Setter Property="OverridesDefaultStyle" Value="True" />
    <Setter Property="Foreground" Value="Transparent" />

    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type DataGridCell}">
                <Border x:Name="BackgroundBorder" 
                        BorderThickness="1">

                    <ContentPresenter VerticalAlignment="Center" 
                                      Margin="4,0,6,0" />
                </Border>

                <ControlTemplate.Triggers>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter Property="Foreground" Value="Black" />
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>                
</Style>

Style可以单独存储在资源中,例如:

  • Window.Resources

  • DataGrid.Style

  • App.xaml - 存储样式的最佳位置

确保控件不直接设置必须在后面的样式中设置的属性,因为本地值的高于优先级而不是setter样式。在这里阅读更多内容:

MSDN: Dependency Property Value Precedence

这就是设置这样的本地值:

<DataGridTextColumn Header="Oct." 
                    Foreground="Black" />

你“割礼”Trigger工作和风格设定者。