码迷,mamicode.com
首页 > Windows程序 > 详细

WPF - Conditional binding with trigger

时间:2015-02-25 07:01:09      阅读:164      评论:0      收藏:0      [点我收藏+]

标签:wpf   trigger   binding   

/* 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!

WPF - Conditional binding with trigger

标签:wpf   trigger   binding   

原文地址:http://blog.csdn.net/garcon1986/article/details/43932223

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!