【问题标题】:Why is my Style.trigger event not working for my WPF Datagrid?为什么我的 Style.trigger 事件不适用于我的 WPF Datagrid?
【发布时间】:2021-05-06 19:49:00
【问题描述】:

我在 WPF 中有一个 DataGrid,并在设置器中为它创建了一个 ControlTemplate。在创建此 ControlTemplate 之前,我的 Style.trigger 与 IsMouseOver 事件完美配合。但是在放置 Controltemplate 之后它不再工作了。这是我的代码:

<Style TargetType="{x:Type DataGridColumnHeader}">
                    <Setter Property="Background" Value="#292F3B"/>
                    <Setter Property="Foreground" Value="LightBlue"/>
                    <Setter Property="FontWeight" Value="SemiBold"/>
                    <Setter Property="Height" Value="30"/>
                    <Setter Property="FontSize" Value="15"/>
                    <Setter Property="BorderThickness" Value="0,0,2,0" />
                    <Setter Property="BorderBrush" Value="#333333"/>
                    <Setter Property="Padding" Value="10 0 0 0"/>
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="{x:Type DataGridColumnHeader}">
                                <Border Background="#242A36">
                                    <Border BorderThickness="1"
                                        CornerRadius="6"
                                        Background="#2D2D30"
                                        Padding="10,0,0,0"
                                        Margin="2">
                                        <ContentPresenter/>
                                    </Border>
                                </Border>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                    <Style.Triggers>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter Property="Background" Value="Red"/>
                        </Trigger>
                    </Style.Triggers>


                </Style>

所以我只希望 IsMouseOver 效果与 ControlTemplate 一起使用。

【问题讨论】:

    标签: wpf data-binding datagrid wpf-controls


    【解决方案1】:

    将您的&lt;Style.Triggers&gt; 移动到现在作为&lt;ControlTemplate.Triggers&gt; 的ControlTemplate 中,您应该会看到它再次改变背景。但是您必须使用 Target 属性来识别背景的目标,因为 WPF 不够聪明,无法知道您在谈论哪个背景。

    这是一个例子:

    <Window.Resources>
        <Style x:Key="mystyle" TargetType="{x:Type DataGridColumnHeader}">
            <Setter Property="Background" Value="#292F3B"/>
            <Setter Property="Foreground" Value="LightBlue"/>
            <Setter Property="FontWeight" Value="SemiBold"/>
            <Setter Property="Height" Value="30"/>
            <Setter Property="FontSize" Value="15"/>
            <Setter Property="BorderThickness" Value="0,0,2,0" />
            <Setter Property="BorderBrush" Value="#333333"/>
            <Setter Property="Padding" Value="10 0 0 0"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type DataGridColumnHeader}">
                        <Border x:Name="border" Background="#242A36">
                            <Border BorderThickness="1"
                                        CornerRadius="6"
                                        Background="#2D2D30"
                                        Padding="10,0,0,0"
                                        Margin="2">
                                <ContentPresenter/>
                            </Border>
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsMouseOver" Value="True">
                                <Setter TargetName="border" Property="Background" Value="Red"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Window.Resources>
    <Grid>
        <DataGrid CanUserAddRows="True" ItemsSource="{Binding Stuff}">
            <DataGrid.Columns>
                <DataGridTextColumn HeaderStyle="{StaticResource mystyle}" Header="Name"/>
            </DataGrid.Columns>
        </DataGrid>
    </Grid>
    

    如果您将鼠标悬停在标题上,这将是如下所示。

    注意:我不知道你想改变什么颜色,所以我只选择了你定义的外边框

    【讨论】:

    • 很高兴听到这个消息,博士。 :D
    猜你喜欢
    • 1970-01-01
    • 2010-10-14
    • 2019-09-07
    • 2015-11-19
    • 1970-01-01
    • 2011-12-03
    • 2012-09-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多