2018-8-10-如何在-UWP-使用-wpf-的-Trigger-
2024-09-03 06:35:18
title | author | date | CreateTime | categories |
---|---|---|---|---|
如何在 UWP 使用 wpf 的 Trigger
|
lindexi
|
2018-08-10 19:16:51 +0800
|
2018-2-13 17:23:3 +0800
|
WPF UWP
|
本文需要告诉大家,如何使用 Behaviors 做出 WPF 的 Trigger ,需要知道 UWP 不支持 WPF 的 Trigger 。
安装 Behaviors
请使用 Nuget 安装,可以输入下面的代码进行安装
Install-Package Microsoft.Xaml.Behaviors.Uwp.Managed
或者搜索 Microsoft.Xaml.Behaviors
下载
他的官网在 Behaviors
以前的代码
在 WPF 开发,可以写出下面代码
<Button>
<Image>
<Image.Style>
<Style TargetType="Image">
<Style.Triggers Property="IsEnabled" Value="False">
<Setter Property="Opacity" Value="0.5"></Setter>
</Style.Triggers>
</Style>
<Image.Style/>
</Image>
</Button>
在 Button IsEnabled 设置图片的透明,但是 UWP 不支持,所以需要使用别的方法。
UWP 使用 Trigger
上面的代码可以很简单用 DataTriggerBehavior 来做。需要知道的是 DataTriggerBehavior 是 Behaviors 的一个东西,所以需要安装之后才可以使用。请看下面的代码。
<Button x:Name="MyButton" Margin="10,10,10,10" Width="140" Height="80">
<Image x:Name="MyImage" Source="Assets/动漫.jpg">
<interactivity:Interaction.Behaviors>
<core:DataTriggerBehavior Binding="{Binding IsEnabled, ElementName=MyButton}" Value="False">
<core:ChangePropertyAction TargetObject="{Binding ElementName=MyImage}" PropertyName="Opacity" Value="0.5" />
</core:DataTriggerBehavior>
</interactivity:Interaction.Behaviors>
</Image>
</Button>
这里的代码不能直接复制使用,需要先添加命名空间和寻找一张图片,因为图片使用的是Assets/动漫.jpg
,所以需要把他修改为你的图片的所在,如何写参见win10 uwp 访问解决方案文件
命名空间 xmlns:Interactivity="using:Microsoft.Xaml.Interactivity"
xmlns:core="using:Microsoft.Xaml.Interactions.Core"
不需要在后台写什么,直接运行可以看到在
按钮可以使用时的图片
按钮无法使用时的图片
请使用 DataTriggerBehavior 的Binding 连到需要修改的属性,在 Value 判断他的值。
然后可以在得到的值判断,修改透明
可以看到使用方法和动画一样
如果使用 MVVM 的话,可以把透明绑到一个属性,通过返回来设置,如果按钮有 IsMyButtonEnabled
那么可以使用下面的代码绑定透明,因为很简单我就不说啦。
return IsMyButtonEnabled ? 1.0 : 0.5;
最新文章
- IntelliJ idea创建Spring MVC的Maven项目
- (String)151. Reverse Words in a String
- npm install -g 全局安装总是出现permission权限问题的解决方案
- Java编译过程、c/c++编译过程区别
- Java中关于 BigDecimal 的一个导致double精度损失的";bug";
- 安装新版本的mysql数据库
- Java序列化 如何把多个对象存储在一个文件中
- Java经典编程题50道之二
- Python并发编程之同步\异步and阻塞\非阻塞
- POJ.3710.Christmas Game(博弈论 树上删边游戏 Multi-SG)
- spring-service.xml 模板
- Windows 环境搭建Redis集群(win 64位)
- HDU1070:Milk
- 编码规范:Eclipse Checkstyle配置
- Loj_6282. 数列分块入门 6
- What’s wrong with virtual methods called through an interface
- [置顶]
 Retrofit2 初印象?
- 第一章 持续集成jenkins工具使用之部署
- [codeforces/edu30]总结(E)
- Ionic学习笔记5_动态组件指令