WPF - Conditional binding with trigger

/* By Dylan SUN*/

WPF conditional binding enables the application to have different behaviors based on a pre-defined condition.

For example, you could use conditional binding to change the background color of your WPF application main window.

Suppose you have the following window declaration in you WPF application.

<Window x:Class="MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:views="clr-namespace:Views"
        xmlns:viewModels="clr-namespace:ViewModels"
        xmlns:controls="clr-namespace:Controls"
        Title="Main Application"
        Height="900"
        Width="1024"
        DataContext="{Binding Main, Source={StaticResource Locator}}">

You could add the following line in the window declaration :

Style="{DynamicResource ResourceKey=MainWindowStyle}"

And use the trigger to change the background color based on the binded property’s (EnvironmentIsSelected) value.

<Style TargetType="{x:Type Window}" x:Key="MainWindowStyle">
<Style.Triggers>
<DataTrigger Binding="{Binding Path=EnvironmentIsSelected}" Value="False">
<Setter Property="Background" Value="Pink"/>
</DataTrigger>
<DataTrigger Binding="{Binding Path=EnvironmentIsSelected}" Value="True">
<Setter Property="Background" Value="{Binding SelectedEnvironment.Color}"/>
</DataTrigger>
</Style.Triggers>
</Style>

I Hope you find this article helpful!

时间: 2024-08-24 17:52:34

WPF - Conditional binding with trigger的相关文章

【转】WPF中Binding的技巧(一)

WPF中Binding的技巧(一) 在WPF应用的开发过程中Binding是一个非常重要的部分. 在实际开发过程中Binding的不同种写法达到的效果相同但事实是存在很大区别的. 这里将实际中碰到过的问题做下汇总记录和理解. 1. source = {binding} 和source = {binding RelativeSource={RelativeSource self},Path=DataContext}效果相同 理解:{binding} 不设定明确的绑定的source,这样bindin

[WPF系列]-基础系列 Trigger, DataTrigger &amp; EventTrigger

So far, we worked with styles by setting a static value for a specific property. However, using triggers, you can change the value of a given property, once a certain condition changes. Triggers come in multiple flavors: Property triggers, event trig

【转载一篇WPF之Binding】WPF之Binding深入探讨

1,Data Binding在WPF中的地位 程序的本质是数据+算法.数据会在存储.逻辑和界面三层之间流通,所以站在数据的角度上来看,这三层都很重要.但算法在3层中的分布是不均匀的,对于一个3层结构的程序来说,算法一般分布在这几处: A.数据库内部. B.读取和写回数据. C.业务逻辑. D.数据展示. E.界面与逻辑的交互. A,B两部分的算法一般都非常稳定,不会轻易去改动,复用性也很高:C处与客户需求最紧密,最复杂,变化最大,大多少算法都集中在这里.D,E负责UI和逻辑的交互,也占有一定量的

WPF之Binding深入探讨

1,Data Binding在WPF中的地位 程序的本质是数据+算法.数据会在存储.逻辑和界面三层之间流通,所以站在数据的角度上来看,这三层都非常重要.但算法在3层中的分布是不均匀的,对于一个3层结构的程序来说,算法一般分布在这几处: A.数据库内部. B.读取和写回数据. C.业务逻辑. D.数据展示. E.界面与逻辑的交互. A,B两部分的算法一般都非常稳定,不会轻易去修改,复用性也非常高:C处与客户需求最紧密,最复杂,变化最大,大多少算法都集中在这里.D,E负责UI和逻辑的交互,也占有一定

WPF之Binding【转】

WPF之Binding[转] 看到WPF如此之炫,也想用用,可是一点也不会呀. 从需求谈起吧: 首先可能要做一个很炫的界面.见MaterialDesignInXAMLToolKit. 那,最主要的呢,还是想显示数据. 就先来数据,数据可以从数据库里得到,可是如何显示出来呢? 本文的主角出来了:(以下内容全转载他人,具体 原文见上面的链接) ==================================== 1,Data Binding在WPF中的地位 程序的本质是数据+算法.数据会在存储.

Initializer for conditional binding must have Optional type, not &#39;String&#39;

今天看到问Swift问题:  Initializer for conditional binding must have Optional type, not 'String' 以前没遇到过这个问题,这不赶紧写个demo验证一下,弄懂什么问题吧,算是积累小知识了. 报错原因: if 里面的判断类型必须是Optional类型. 解决方法: 注意不仅是if let会报错,guard let 同样是这样... Initializer for conditional binding must have

WPF之Binding对数据的转换(第五天)

Binding在Slider控件与TextBox控件之间建立关联,值可以互相绑定,但是它们的数据类型是不同的,Slider是Double类型,Text为String.原来,Binding有一种机制称为数据转换(Data Converter),当数据绑定的源与目标不同类型时,处理比较简单时,系统就自动的进行了类型转换,但是对于相对复杂的类型转换时,就需要我们手动进行了. 下面用一个例子来说明Convert的应用,程序的用途是在列表里面向玩家显示一些球的状态. 首先创建几个自定义数据类型: publ

initializer for conditional binding must have optional type not AVAudioPlayer

if let buttonBeep = self.setupAudioPlayerWithFile("ButtonTap", type: "wav") {                        self.buttonBeep = buttonBeep        }        if let secondBeep = self.setupAudioPlayerWithFile("SecondBeep", type: "wav

WPF之Binding

Bingding是什么 WPF中的Binding注重表达的是一种像桥梁一样的关系,它的两段分别是Binding的源(Source)和目标(Target).数据从哪里来哪里就是源,Binding是架在中间的桥梁,Binding的目标是数据要往哪去儿.一般情况下,Binding源是逻辑层的对象,Binging的目标是UI层的控件对象. Binding如何传递数据变化 前面说过数据源是一个对象,但一个对象身上可能有很多数据,这些数据又通过属性暴露给外界.其中你想通过Binding送达UI元素的数据,或