c# - 在 WPF MaterialDesign 中悬停自定义控件

标签 c# wpf xaml hover material-design

我想为控件添加悬停效果(更改边框或背景颜色即可)。我找到了很多关于它的答案,比如这个: WPF: On Mouse hover on a particular control, increase its size and overlap on the other controls

问题是我正在使用自定义控件(我正在专门为 wpf 使用 materialdesign)。我什至不知道在 TargetType 上放什么。

更新:这是我目前所做的。我已经删除了不相关的代码。

正如我所说,我不知道要在 TargetType 上放什么,所以我试着放上 Control 但它不起作用。

<md:Card 
    Margin="4 4 4 4" 
    Width="100" 
    Height="220"
    >
    <md:Card.Style>
        <Style TargetType="{x:Type Control}">
            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="Background" Value="Red"/>
                </Trigger>
            </Style.Triggers>
        </Style>
    </md:Card.Style>
    <Grid>
    </Grid>
</md:Card>

最佳答案

试试这个:

<Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes" 
    xmlns:conv="clr-namespace:MaterialDesignThemes.Wpf.Converters;assembly=MaterialDesignThemes.Wpf"
    xmlns:local="clr-namespace:WpfApplication1"
    x:Class="WpfApplication1.MainWindow"
    mc:Ignorable="d"
    Title="MainWindow" Height="300" Width="300">
<Window.Resources>


    <Style x:Key="CardStyle1" TargetType="{x:Type materialDesign:Card}">
        <Setter Property="Background" Value="#2fff0000"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type materialDesign:Card}">
                    <Grid Margin="{TemplateBinding Margin}" Background="Transparent">
                        <AdornerDecorator>
                            <AdornerDecorator.CacheMode>
                                <BitmapCache EnableClearType="True" SnapsToDevicePixels="True"/>
                            </AdornerDecorator.CacheMode>
                            <Border Effect="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=(materialDesign:ShadowAssist.ShadowDepth), Converter={x:Static conv:ShadowConverter.Instance}}"
                    CornerRadius="{TemplateBinding UniformCornerRadius}">
                                <Border Background="{TemplateBinding Background}" Padding="{TemplateBinding Padding}" 
                        x:Name="PART_ClipBorder"
                        Clip="{TemplateBinding ContentClip}" />
                            </Border>
                        </AdornerDecorator>
                        <ContentPresenter 
            x:Name="ContentPresenter"                    
            Margin="{TemplateBinding Padding}"
            Clip="{TemplateBinding ContentClip}"
            Content="{TemplateBinding ContentControl.Content}" 
            ContentTemplate="{TemplateBinding ContentControl.ContentTemplate}" 
            ContentTemplateSelector="{TemplateBinding ContentControl.ContentTemplateSelector}" 
            ContentStringFormat="{TemplateBinding ContentControl.ContentStringFormat}">
                        </ContentPresenter>
                    </Grid>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter TargetName="PART_ClipBorder" Property="Background" Value="#4fff0000" />
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

</Window.Resources>
<Grid>

    <materialDesign:Card Style="{DynamicResource CardStyle1}" 
                         Content="My Sample Card" 
                         HorizontalAlignment="Center" 
                         Margin="0" 
                         VerticalAlignment="Center" 
                         Width="100" 
                         Height="100" />

</Grid>

enter image description here

关于c# - 在 WPF MaterialDesign 中悬停自定义控件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39714754/

相关文章:

JAVA LDAP 错误 javax.naming.NamingException : [LDAP: error code 1 - 000004DC: LdapErr: DSID-0C09075A

silverlight - Windows phone 7 滚动查看器问题

c# - 多层次数据模板

c# - WPF 2 标签宽度响应窗口调整大小

c# uwp 模糊屏幕的一部分

c# - 如何为 C# 预处理器符号定义值?

c# - 引用类库项目中的 System.Diagnostics.Trace

c# - 如何使用 Application Insights 覆盖或忽略 cloud_RoleInstance

.net - WPF中的两种百分比格式绑定(bind)

c# - 在代码中使用 XAML resourceDictionary