Use Data Triggers to Change the Appearance of Bound Data : DataTrigger « Windows Presentation Foundation « C# / CSharp Tutorial

Home
C# / CSharp Tutorial
1.Language Basics
2.Data Type
3.Operator
4.Statement
5.String
6.struct
7.Class
8.Operator Overload
9.delegate
10.Attribute
11.Data Structure
12.Assembly
13.Date Time
14.Development
15.File Directory Stream
16.Preprocessing Directives
17.Regular Expression
18.Generic
19.Reflection
20.Thread
21.I18N Internationalization
22.LINQ
23.GUI Windows Forms
24.Windows Presentation Foundation
25.Windows Communication Foundation
26.Workflow
27.2D
28.Design Patterns
29.Windows
30.XML
31.XML LINQ
32.ADO.Net
33.Network
34.Directory Services
35.Security
36.unsafe
C# / C Sharp
C# / C Sharp by API
C# / CSharp Open Source
C# / CSharp Tutorial » Windows Presentation Foundation » DataTrigger 
24.143.3.Use Data Triggers to Change the Appearance of Bound Data
<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:WpfApplication1="clr-namespace:WpfApplication1"
    x:Name="thisWindow" Title="WPF" Height="240" Width="280">

    <Window.Resources>
        <WpfApplication1:DataItems x:Key="dataItems"/>
        <WpfApplication1:AmountToHeightConverter x:Key="amountToHeightConverter" />
        <DataTemplate x:Key="dataItemtemplate">
            <Rectangle x:Name="rectangle" Width="30"
                VerticalAlignment="Bottom"
                Fill="Green"
                Height="{Binding Path=Amount,Converter={StaticResource amountToHeightConverter}}"/>
            <DataTemplate.Triggers>
                <DataTrigger Binding="{Binding Path=IsPositive}" Value="False">
                    <Setter TargetName="rectangle" Property="Fill" Value="Red"/>
                </DataTrigger>
            </DataTemplate.Triggers>
        </DataTemplate>
    </Window.Resources>
    <StackPanel>
        <ItemsControl ItemsSource="{Binding Source={StaticResource dataItems}}"
            ItemTemplate="{StaticResource dataItemtemplate}">
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <StackPanel Orientation="Horizontal"/>
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
        </ItemsControl>

        <Line Stroke="Black" StrokeThickness="2" X1="0" Y1="0" X2="0" Y2="{Binding ElementName=thisWindow, Path=ActualHeight}"/>
        <Line Stroke="Black" StrokeThickness="2" X1="0" Y1="0" X2="{Binding ElementName=thisWindow, Path=ActualWidth}" Y2="0"/>
    </StackPanel>
</Window>
//File:Window.xaml.cs
using System;
using System.Windows.Data;
using System.Globalization;
using System.Collections.ObjectModel;
namespace WpfApplication1
{
    [ValueConversion(typeof (double), typeof (double))]
    public class AmountToHeightConverter : IValueConverter
    {
        public Object Convert(Object value,Type targetType,Object parameter,CultureInfo culture)
        {
            double amount = System.Convert.ToDouble(value);
            if(amount < 0)
                amount = 0;

            return amount;
        }
        public object ConvertBack(object value,Type targetType,object parameter,CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
    public class DataItem
    {
        public double Amount
        {
            get;
            set;
        }

        public bool IsPositive
        {
            get
            {
                return Amount >= 0;
            }
        }
    }

    public class DataItems : Collection<DataItem>
    {
        public DataItems()
        {
            this.Add(new DataItem(){Amount=5});
            this.Add(new DataItem(){Amount=8});
            this.Add(new DataItem(){Amount=-5});
            this.Add(new DataItem(){Amount=2});
            this.Add(new DataItem(){Amount=-5});
            this.Add(new DataItem(){Amount=-5});
        }
    }
}
WPF Use Data Triggers To Change The Appearance Of Bound Data
24.143.DataTrigger
24.143.1.Data Trigger SampleData Trigger Sample
24.143.2.Use DataTrigger and MultiDataTrigger.Use DataTrigger and MultiDataTrigger.
24.143.3.Use Data Triggers to Change the Appearance of Bound DataUse Data Triggers to Change the Appearance of Bound Data
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.