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;

参见:Trigger element (XAML) is not supported in a UWP project

最新文章

  1. IntelliJ idea创建Spring MVC的Maven项目
  2. (String)151. Reverse Words in a String
  3. npm install -g 全局安装总是出现permission权限问题的解决方案
  4. Java编译过程、c/c++编译过程区别
  5. Java中关于 BigDecimal 的一个导致double精度损失的&quot;bug&quot;
  6. 安装新版本的mysql数据库
  7. Java序列化 如何把多个对象存储在一个文件中
  8. Java经典编程题50道之二
  9. Python并发编程之同步\异步and阻塞\非阻塞
  10. POJ.3710.Christmas Game(博弈论 树上删边游戏 Multi-SG)
  11. spring-service.xml 模板
  12. Windows 环境搭建Redis集群(win 64位)
  13. HDU1070:Milk
  14. 编码规范:Eclipse Checkstyle配置
  15. Loj_6282. 数列分块入门 6
  16. What’s wrong with virtual methods called through an interface
  17. [置顶] Retrofit2 初印象?
  18. 第一章 持续集成jenkins工具使用之部署
  19. [codeforces/edu30]总结(E)
  20. Ionic学习笔记5_动态组件指令

热门文章

  1. linux 添加文字、图形、线条、箭头的 截图
  2. CF274D
  3. Ubuntu无法连接无线网
  4. spring boot + mybatis 访问 neo4j
  5. C#中App.config文件配置获取
  6. JavaScript--淘宝图片切换
  7. 2016中国银行Top100榜单发布 工行排首位
  8. poj 2184 01背包变形【背包dp】
  9. MaxCompute客户端(odpscmd)在windows命令行下查询中文乱码问题处理实践
  10. shell学习(17)- shell中2&gt;&amp;1的解释及输入输出重定向