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

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Design Patterns
8.Development Class
9.Event
10.File Stream
11.Generics
12.GUI Windows Form
13.Language Basics
14.LINQ
15.Network
16.Office
17.Reflection
18.Regular Expressions
19.Security
20.Services Event
21.Thread
22.Web Services
23.Windows
24.Windows Presentation Foundation
25.XML
26.XML LINQ
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source
C# / C Sharp » Windows Presentation Foundation » DataTriggerScreenshots 
Use Data Triggers to Change the Appearance of Bound Data
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});
        }
    }
}

   
    
  
Related examples in the same category
1.Data Trigger SampleData Trigger Sample
2.Use DataTrigger and MultiDataTrigger.Use DataTrigger and MultiDataTrigger.
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.