| |
24.32.9.DataTrigger, ListBox and user object |
|
<Window x:Class="DataTriggerSample.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:c="clr-namespace:DataTriggerSample"
Title="DataTriggerSample" Height="300" Width="300">
<Window.Resources>
<c:People x:Key="PeopleData"/>
<Style TargetType="{x:Type ListBoxItem}">
<Style.Triggers>
<DataTrigger Binding="{Binding Path=Name}" Value="A">
<Setter Property="Background" Value="Yellow" />
</DataTrigger>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding Path=Name}" Value="S" />
</MultiDataTrigger.Conditions>
<MultiDataTrigger.Setters>
<Setter Property="Background" Value="LightGreen" />
</MultiDataTrigger.Setters>
</MultiDataTrigger>
</Style.Triggers>
</Style>
<DataTemplate DataType="{x:Type c:Person}">
<Canvas Width="260" Height="20">
<TextBlock FontSize="12" Width="130" Canvas.Left="0" Text="{Binding Path=Name}"/>
</Canvas>
</DataTemplate>
</Window.Resources>
<StackPanel>
<TextBlock FontSize="18" Margin="5" FontWeight="Bold"
HorizontalAlignment="Center">Data Trigger Sample</TextBlock>
<ListBox Width="250" HorizontalAlignment="Center" Background="White"
ItemsSource="{Binding Source={StaticResource PeopleData}}"/>
</StackPanel>
</Window>
//File:Window.xaml.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
namespace DataTriggerSample
{
public partial class Window1 : System.Windows.Window
{
public Window1()
{
InitializeComponent();
}
}
public class Person
{
private string _name;
public string Name
{
get { return _name; }
set { _name = value; }
}
public Person(string name)
{
this._name = name;
}
}
public class People : ObservableCollection<Person>
{
public People()
{
Add(new Person("A"));
Add(new Person("B"));
Add(new Person("C"));
}
}
}
|
|
|
|