IsMouseOver触发器不适用于GridViewColumn WPF XAML

时间:2015-04-23 00:24:26

标签: c# wpf xaml

以下是XAML的代码:

<Page.Resources>
        <Style x:Key="cells" TargetType="GridViewColumnHeader">
            <Style.Triggers>
                <Trigger Property="IsEnabled" Value="True">
                    <Setter Property="Background" Value="#FF00B9FF"></Setter>
                    <Setter Property="Foreground" Value="White"></Setter>
                    <Setter Property="BorderBrush" Value="#FF00B9FF"></Setter>
                    <Setter Property="Padding" Value="8"></Setter>
                    <Setter Property="MinWidth" Value="100"></Setter>
                </Trigger>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="Background" Value="#FF00B9FF"></Setter>
                    <Setter Property="Foreground" Value="White"></Setter>
                    <Setter Property="BorderBrush" Value="#FF00B9FF"></Setter>
                    <Setter Property="Padding" Value="2"></Setter>
                    <Setter Property="MinWidth" Value="100"></Setter>
                </Trigger>
            </Style.Triggers>

        </Style>
    </Page.Resources>
    <Grid>
        <Label Content="Notifications" VerticalAlignment="Top" HorizontalAlignment="Left" FontSize="20"></Label>
        <ListView Background="{x:Null}" FontSize="17" Margin="0,30,0,0" ItemsSource="{Binding Notifications}" HorizontalAlignment="Center" BorderBrush="{x:Null}">
            <ListView.View>
                <GridView AllowsColumnReorder="False">
                    <GridViewColumn Header="Sl No." DisplayMemberBinding="{Binding Slno}" HeaderContainerStyle="{StaticResource cells}"></GridViewColumn>
                    <GridViewColumn Header="Message" DisplayMemberBinding="{Binding Message}" HeaderContainerStyle="{StaticResource cells}"></GridViewColumn>
                    <GridViewColumn Header="Date" DisplayMemberBinding="{Binding Date}" HeaderContainerStyle="{StaticResource cells}"></GridViewColumn>
                </GridView>
            </ListView.View>
        </ListView>
    </Grid>

IsEnabled触发器很好,但IsMouseOver触发器不起作用。我尝试使用ControlTemplate,但<GridViewColumn/>没有属性,并返回错误Cannot convert ControlTemplate type to DataTemplate or Style

我正在尝试更改网格标题的样式,当在MOuseOver上显示默认模板时。

如何覆盖样式?

1 个答案:

答案 0 :(得分:2)

您的触发器工作正常,只是您没有看到您的期望,您需要在设置器中设置ContolTemplate,例如

    <Trigger Property="IsMouseOver" Value="True">
           <Setter Property="Template">
                 <Setter.Value>
                      <ControlTemplate TargetType="{x:Type GridViewColumnHeader}">
                      <Border BorderBrush="#FF00B9FF" Background="#FF00B9FF">
                             <TextBlock Padding="5,5,5,5" Text="{TemplateBinding Content}" TextAlignment="Center" />
                       </Border>
                   </ControlTemplate>
                 </Setter.Value>
            </Setter>
            <Setter Property="Padding" Value="2"></Setter>
    </Trigger>

另请注意,您可以在GridView中设置一次ColumnHeaderStyle,而不是为所有标题设置多次,例如:

<GridView AllowsColumnReorder="False" ColumnHeaderContainerStyle="{StaticResource cells}">