首页 新闻 会员 周边

wpf button 小问题

0
悬赏园豆:60 [已解决问题] 解决于 2013-12-19 13:34

各位园友:

  随便拽一个button进工程里,想实现button 在IsMouseOver的时候,底色不变,请问如何搞,我在属性触发器里添加了相应的代码,设置Background为透明,但是button上面还是罩了一层颜色。请园友解惑:

样式代码:

 <Style x:Key="BtnExample" TargetType="Button">
        <Setter Property="Background">
            <Setter.Value>
                <ImageBrush ImageSource="/EnvrimentConfig;component/Images/get.ico" />
            </Setter.Value>               
        </Setter>
        <Style.Triggers>           
        <Trigger Property="Button.IsMouseOver" Value="True">
                <Setter Property="Button.Background" Value="Transparent" >
                </Setter>
            </Trigger>
        </Style.Triggers>
    </Style>


XAML下代码:

  <Button  Height="96" Style="{StaticResource BtnExample}"  HorizontalAlignment="Left" Margin="170,12,0,0" Name="button3" VerticalAlignment="Top" Width="90" Grid.RowSpan="2">
问题补充:

是大家觉得简单么?!我加分行吧~

抓住青虫的主页 抓住青虫 | 初学一级 | 园豆:26
提问于:2012-07-16 15:17
< >
分享
最佳答案
0
View Code
 1 <Button Height="35" Width="85" VerticalAlignment="Bottom" HorizontalAlignment="Right" Content="删除" Click="Button_Click">
 2             <Button.Style>
 3                 <Style TargetType="{x:Type Button}">
 4                     <Style.Triggers>
 5                         <Trigger Property="IsMouseOver" Value="true">
 6                             <Setter Property="Background" Value="Red"/>
 7                         </Trigger>
 8                     </Style.Triggers>
 9                 </Style>
10             </Button.Style>
11         </Button>

你试试看。

收获园豆:20
Bj-Soctt-胖仔 | 初学一级 |园豆:70 | 2012-07-16 18:27

这个肯定不行,你代码跟我代码没实质性区别。没有其他人知道了么~~

抓住青虫 | 园豆:26 (初学一级) | 2012-07-16 19:16
其他回答(5)
0

这问题度娘应该不少吧,可考虑自定义Template;

当然了,有时间,博主可参考以下网址

http://www.chinaz.com/program/2008/0715/33006.shtml

收获园豆:20
八戒的师傅 | 园豆:1472 (小虾三级) | 2012-07-17 19:38

说来惭愧,我确实加了模板,但是想实现某杀毒软件(偷偷告诉你,361-1)的效果,鼠标未挪到软件上,就是一图标,挪上去之后,有一层淡淡的矩形绘在图标上,但是图标还看的很清晰,哎,为了这个效果我费了老劲了,因为刚接触wpf不久,搞的不是很精,所以希望在这一块有造诣的园友指点一下

支持(0) 反对(0) 抓住青虫 | 园豆:26 (初学一级) | 2012-07-17 22:21
0

在wpf中,有一个创造性的概念叫做VSM(Visual State Manager),你可以使用blend,然后通过可视化工具直接做出在各种状态下(包括鼠标悬停)的效果。

ocean | 园豆:824 (小虾三级) | 2012-07-18 09:10

兄台,你所说这个blend能实现这样效果么

鼠标没碰按钮

鼠标悬浮在按钮上

支持(0) 反对(0) 抓住青虫 | 园豆:26 (初学一级) | 2012-07-19 14:08
0

曾经写过一个,不知是否符合lz要求:

鼠标没碰:

鼠标放上后:

lz可以自行修改,改变背景色,边框之类的 

<Style TargetType="{x:Type Button}">
        <Setter Property="Background" Value="{StaticResource BgControlSecond}"/>
        <Setter Property="HorizontalContentAlignment" Value="Center"/>
        <Setter Property="VerticalContentAlignment" Value="Center"/>
        <Setter Property="Padding" Value="1"/>
        <Setter Property="Foreground" Value="{StaticResource Foreground}"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Button}">
                    <Grid>
                    <Border x:Name="background" Height="{TemplateBinding Height}" Width="{TemplateBinding Width}" Opacity="0"
                                Background="{TemplateBinding Background}"
                                BorderBrush="{StaticResource Foreground}"
                                BorderThickness="1" CornerRadius="2">
                    </Border>
                        <Grid>
                            <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                            <Rectangle x:Name="DisabledVisualElement" Fill="#FFFFFFFF" IsHitTestVisible="false" Opacity="0" RadiusY="3" RadiusX="3"/>
                        </Grid>
                    </Grid>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsMouseOver" Value="true">
                            <Setter Property="Opacity" Value="1" TargetName="background"/>
                        </Trigger>
                        <Trigger Property="IsPressed" Value="true">
                            <Setter Property="Background" Value="{StaticResource BgControl}"/>
                        </Trigger>
                        <Trigger Property="IsEnabled" Value="false">
                            <Setter Property="Opacity" Value="0.55" TargetName="DisabledVisualElement"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

收获园豆:20
蒲西 | 园豆:224 (菜鸟二级) | 2012-07-19 21:19
0

其实就是触发器起的作用,后来借鉴了朋友的例子搞定了,一年了才结贴,死忙死忙的···

抓住青虫 | 园豆:26 (初学一级) | 2013-12-19 13:33
0

怎么解决的,兄台?

熙蔺先生 | 园豆:202 (菜鸟二级) | 2014-10-05 18:26
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册