wpf XMAL中隐藏控件

原文: wpf XMAL中隐藏控件

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/a771948524/article/details/9264569

首先,需要解决怎样在Click事件后设置Visibility属性?WPF中的EventSetter只能连接事件和事件处理的方法。而EventTrigger则只能定义TriggerAction,而不能添加任何SetterBase。

解决方案就是用一个关键帧动画,这样可以用在EventTrigger中,接着用一个DiscreteObjectKeyFrame来设置属性值就可以了。这个关键帧的KeyTime是0:0:0,这样在动画开始的时候,这个值会被立即设置。

接着怎样引用Visibility的枚举值?用x:Static标记扩展就可以了

代码:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="263">
    <Grid>
        <StackPanel>
            <Button Content="Click Hide">
                <Button.Triggers>
                    <EventTrigger RoutedEvent="Button.Click">
                        <EventTrigger.Actions>
                            <BeginStoryboard>
                                <Storyboard Storyboard.TargetName="text"
                                    Storyboard.TargetProperty="Visibility">
                                    <ObjectAnimationUsingKeyFrames>
                                        <DiscreteObjectKeyFrame Value="{x:Static Visibility.Hidden}" KeyTime="0:0:0"/>
                                    </ObjectAnimationUsingKeyFrames>
                                </Storyboard>
                            </BeginStoryboard>
                        </EventTrigger.Actions>
                    </EventTrigger>
                </Button.Triggers>
            </Button>
            <Button Name="text" Height="20" Width="40" Margin="18,30,113,161">
                zxl</Button>
        </StackPanel>
    </Grid>
        
</Window>

效果:





posted on 2018-10-21 13:52 NET未来之路 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/lonelyxmas/p/9824829.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值