吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 987|回复: 2
收起左侧

[求助] C#新手 求解样式触发器问题

[复制链接]
不ok个男人 发表于 2021-3-11 16:44
控件代码如下
[Asm] 纯文本查看 复制代码
<StackPanel HorizontalAlignment="Right"  Orientation="Horizontal" MinWidth="100" >                    <!--添加按钮-->
                  <Button  Style="{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"/>
                  <!--选项按钮-->
                  <Button  Style="{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}">
                      <Grid  Height="17"   Width="35">
                          <Image Source="关闭.png"  RenderOptions.BitmapScalingMode="Fant" />
                      </Grid>
                  </Button>
              </StackPanel>
样式如下:
[Asm] 纯文本查看 复制代码
<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" >
                            <ContentControl  Height="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 17:37
本帖最后由 ForUsContribute 于 2021-3-11 18:00 编辑

[C#] 纯文本查看 复制代码
<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" >
                            <ContentControl  Height="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:06
本帖最后由 Je11y 于 2021-3-11 18:15 编辑

[C#] 纯文本查看 复制代码
        <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" >
                            <ContentControl  Height="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
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-11-26 06:47

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表