C#新手 求解样式触发器问题
控件代码如下<StackPanel HorizontalAlignment="Right"Orientation="Horizontal" MinWidth="100" > <!--添加按钮--><ButtonStyle="{StaticResource TitelButtonStyle}" >
<Grid>
<Image Source="添加.png" HorizontalAlignment="Left" RenderOptions.BitmapScalingMode="Fant" />
<TextBlock Margin="0,1,0,0" Foreground="White" Text="添加" HorizontalAlignment="Right" Width="28 "/>
</Grid>
</Button>
<Grid Width="1" Height="20" Background="White"Opacity="0.5"Margin="10,0,0,0"/>
<!--选项按钮-->
<ButtonStyle="{StaticResource TitelButtonStyle}" >
<Grid>
<Image Source="齿轮.png" HorizontalAlignment="Left" RenderOptions.BitmapScalingMode="Fant" />
<TextBlock Margin="0,1,0,0" Text="选项" HorizontalAlignment="Right" Width="28 "/>
</Grid>
</Button>
<Grid Width="1" Height="20" Background="White" Opacity="0.5"Margin="10,0,0,0"/>
<!--关闭按钮-->
<Button Style="{StaticResource CloseButton}">
<GridHeight="17" Width="35">
<Image Source="关闭.png"RenderOptions.BitmapScalingMode="Fant" />
</Grid>
</Button>
</StackPanel>样式如下:
<Style x:Key="CloseButton" TargetType="Button">
<Setter Property="Opacity" Value="0.5"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Border Background="Transparent" BorderThickness="0" >
<ContentControlHeight="30" Width="35" HorizontalAlignment="Center" VerticalAlignment="Center"Content="{TemplateBinding Content}" Background="{TemplateBinding Background}"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="#ff0000"/>
<Setter Property="Opacity" Value="1" />
</Trigger>
</Style.Triggers>
</Style>
关闭按钮应用的样式中,鼠标悬停触发器更改了两个属性,Opacity更改了可以生效,Background变更却没效果,求大神答疑 本帖最后由 ForUsContribute 于 2021-3-11 18:00 编辑
<Style x:Key="CloseButton" TargetType="Button">
<Setter Property="Opacity" Value="0.5"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Border Background="{TemplateBinding Background}" BorderThickness="0" >
<ContentControlHeight="30" Width="35" HorizontalAlignment="Center" VerticalAlignment="Center"Content="{TemplateBinding Content}"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="#ff0000"/>
<Setter Property="Opacity" Value="1" />
</Trigger>
</Style.Triggers>
</Style>
本帖最后由 Je11y 于 2021-3-11 18:15 编辑
<Style x:Key="CloseButton" TargetType="Button">
<Setter Property="Opacity" Value="0.5"/>
<Setter Property="Background" Value="Transparent"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Border x:Name="border" Background="{TemplateBinding Background}" BorderThickness="0" >
<ContentControlHeight="30" Width="35" HorizontalAlignment="Center" VerticalAlignment="Center"Content="{TemplateBinding Content}"/>
</Border>
<ControlTemplate.Triggers >
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="#ff0000" TargetName="border"/>
<Setter Property="Opacity" Value="1" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
优先级的问题:
https://docs.microsoft.com/en-us/dotnet/desktop/wpf/advanced/dependency-property-value-precedence?redirectedfrom=MSDN&view=netframeworkdesktop-4.8
页:
[1]