Wpf-conditional Binding with Trigger

Source: Internet
Author: User

/* by Dylan sun*/

WPF conditional binding enables the application to has 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 has the following window declaration in your 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=" the"        Width="1024x768"        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.

<StyleTargettype="{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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.