gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 21:45:56 39 4
gpt4 key购买 nike

我想为控件添加悬停效果(更改边框或背景颜色即可)。我找到了很多关于它的答案,比如这个: 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/

39 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com